Yii:引入DEV和PROD环境


Yii: introduce DEV and PROD environments

我想写Yii代码,这在DEV和PROD环境中是不同的。例如,在PROD上,我希望应用程序发送真正的电子邮件,而在DEV上,将所有内容写入文件或发送到本地邮箱。

如果在DEV上启用db分析,在PROD上禁用db分析,那就太好了。

有完成任务的方法吗?

也许这个扩展可以帮助你:http://www.yiiframework.com/extension/yii-environment/

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

我只是根据主机包含基本(通用)配置的不同配置文件。

覆盖特定环境的db密码的示例:

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'
         )
      )
   )
); 

基本上这给了你一个基于服务器主机的自定义配置文件。显然,如果您使用这个,您应该验证主机名和配置是否存在。

您还可以使用其他东西(如环境的定义等)。

基本原则保持不变:"最终"配置文件包括公共配置,删除不需要的东西(如果适用),并定义一个数组结构,只包含需要更改的内容。该结构将与公共配置合并,从而产生最终配置。