H
H
HaruAtari2013-12-28 13:30:25
Yii
HaruAtari, 2013-12-28 13:30:25

PHP: How to distinguish test and production version of an application in the console?

Good afternoon.
There is a site on yii. And as it should be, all versions of the configuration: one is a test version, the second is a working one. They differ in the active database and system logs.
How to connect the required configuration for the web part is clear - you can distinguish it by the host name.

if ($_SERVER['HTTP_HOST'] === '192.168.0.100') {
    define('YII_DEBUG', true);
    ini_set('display_errors', 1);
    error_reporting(E_ALL);
}

And how to distinguish on which server the application is located during the launch of console scripts from cron?

Answer the question

In order to leave comments, you need to log in

4 answer(s)
P
Pavel Virsky, 2013-12-28
@HaruAtari

You can create a file in the root for the test environment, for example, "development". And then something like this (index.php):

if(file_exists('development')){

  defined('YII_DEBUG') or define('YII_DEBUG', true);
  defined('YII_TRACE_LEVEL') or define('YII_TRACE_LEVEL', 3);

  $yii = dirname(__FILE__) . '/../../yii/framework/yii.php';

  $config = dirname(__FILE__) . '/protected/config/dev.php';

}else{

  defined('YII_DEBUG') or define('YII_DEBUG', false);

  $yii = dirname(__FILE__) . '/../../../yii/framework/yiilite.php';
  $config = dirname(__FILE__) . '/protected/config/production.php';

}

M
Mikhail Osher, 2013-12-28
@miraage

Create environment variables in apache/nginx setup and in scripts use

if ($_SERVER['APP_ENV'] === 'dev') { // или prod, test
   // ....
}

O
OnYourLips, 2013-12-28
@OnYourLips

bash prompt color.

S
Sergey, 2013-12-28
Protko @Fesor

yii has no division into environments per se. There is only an internal variable YII_DEBUG which affects only framework components by default. Logging settings and separating configs by environment is your concern, you can do this either by checking the YII_DEBUG constant or on your own (different configs for different environments, array_merge spells, etc.).
I use the same approach for my projects as in symfony. Configs are stored in yml, common parts are in config.yml, entry point loads config_prod or config_dev depending on the required environment. Then the bootstrapper itself compiles the config, or takes it from the cache, that's all.
As for console commands, it's more logical to run them in the dev environment (let's say that the influence of caching some data would be leveled, or whatever your settings may differ there). You can also add some additional argument ala --debug or --no-debug to the entry point of the console application to switch the environment.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question