简体   繁体   中英

Yii: introduce DEV and PROD environments

I would like to write Yii code, which differs on DEV and PROD environments. For example, on PROD I would like application to send real e-mails, while on DEV write everything to file or send to local mailbox.

Also it would be nice to have db profiling enabled on DEV and disabled on PROD.

Is there a way to accomplish the task?

也许该扩展程序可以帮助您: http : //www.yiiframework.com/extension/yii-environment/

有一个不同的配置文件,并让所有其他源相同。

I just include different config files based on the host that include the basic (common) config.

An example that overrides the db password for a specific environment:

index.php :

$configFile = $_SERVER['SERVER_NAME'] . '.php';
if ($configFile == '.php') $configFile = 'main.php';
$configFile = "$baseDir/config/$configFile";
$app = Yii::createWebApplication($configFile);
$app->run();

main.php :

return array(
   ...
   'components' => array(
      'db' => array(
        'connectionString'            => 'mysql:host=localhost;dbname=mydb',
        'username'                    => 'some_user',
        'password'                    => 'some_password',
      )
   )
);

production.server.com.php :

// Include common config
$config = require(dirname(__FILE__) . '/main.php');
// Remove whatever we don't need here (obviously optional)
unset($config['components']['...']);

return CMap::mergeArray(
   $config,
   array(
      'components' => array(
         'db' => array(
            'password' => 'alternative_production_password'
         )
      )
   )
); 

So basically this gives you a custom config file based on the server host. Obviously if you use this you should verify the host name and whether the config exists.

You could also use other things (like a define for the environment or so).

The basic principle remains the same: The "final" config file includes the common configuration, removes unneeded things (if applicable) and defines an array structure that only contains what needs to be changed. This structure is merged with the common config to result in the final configuration.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM