How to determine the environment in the CLI?

I have different configuration files for a script that run on both dev and the production server. The question is, can I somehow determine what exactly?

+5
source share
3 answers

You have another bootstrap file to enable, depending on the server you are on. So

include "bootstrap.php";
// your script etc..

In the production window, bootstrap.php will have a constant IS_DEV = FALSE In the development window, bootstrap.php will have the same constantIS_DEV = TRUE

Thus, the same script will contain the same bootstrap.php file name, but the contents of the bootstrap will have different values ​​for the constant IS_DEV.

+6
source

Some solutions to choose from:

  • config.ini config.ini.dist (.dist , )
  • .htaccess , getenv
  • - ,
  • script
  • IP-

, , , .

:

.htaccess:

.htaccess ( ):

SetEnv APPLICATION_ENVIRONMENT staging

PHP script:

if ('staging' === getenv('APPLICATION_ENVIRONMENT')) {
    // use config_staging.ini
} else {
   // use config_develop.ini
}

, , .

, , Zend Framework.


( ), :

export APPLICATION_ENVIRONMENT=development

Windows:

SET APPLICATION_ENVIRONMENT=development

getenv PHP.

+2
if (PHP_SAPI === 'cli') {
  echo 'running in console!';
}
-1
source

All Articles