Answer the question
In order to leave comments, you need to log in
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);
}
Answer the question
In order to leave comments, you need to log in
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';
}
Create environment variables in apache/nginx setup and in scripts use
if ($_SERVER['APP_ENV'] === 'dev') { // или prod, test
// ....
}
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 questionAsk a Question
731 491 924 answers to any question