Php Yii:介绍开发和产品环境

Php Yii:介绍开发和产品环境,php,yii,development-environment,production-environment,Php,Yii,Development Environment,Production Environment,我想写Yii代码,这在DEV和PROD环境中是不同的。例如,在PROD上,我希望应用程序发送真实的电子邮件,而在DEV上,我希望将所有内容写入文件或发送到本地邮箱 另外,在DEV上启用db评测,在PROD上禁用db评测也很好 有办法完成任务吗?有一个不同的配置文件,让所有其他源都相同 也许这个扩展可以帮助您: 我只是根据包含基本(公共)配置的主机包括不同的配置文件 覆盖特定环境的db密码的示例: index.php: $configFile = $_SERVER['SERVER_NAME'] .

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

另外,在DEV上启用db评测,在PROD上禁用db评测也很好


有办法完成任务吗?

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

也许这个扩展可以帮助您:

我只是根据包含基本(公共)配置的主机包括不同的配置文件

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

index.php

$configFile = $_SERVER['SERVER_NAME'] . '.php';
if ($configFile == '.php') $configFile = 'main.php';
$configFile = "$baseDir/config/$configFile";
$app = Yii::createWebApplication($configFile);
$app->run();
return array(
   ...
   'components' => array(
      'db' => array(
        'connectionString'            => 'mysql:host=localhost;dbname=mydb',
        'username'                    => 'some_user',
        'password'                    => 'some_password',
      )
   )
);
// 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'
         )
      )
   )
); 
main.php

$configFile = $_SERVER['SERVER_NAME'] . '.php';
if ($configFile == '.php') $configFile = 'main.php';
$configFile = "$baseDir/config/$configFile";
$app = Yii::createWebApplication($configFile);
$app->run();
return array(
   ...
   'components' => array(
      'db' => array(
        'connectionString'            => 'mysql:host=localhost;dbname=mydb',
        'username'                    => 'some_user',
        'password'                    => 'some_password',
      )
   )
);
// 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'
         )
      )
   )
); 
production.server.com.php

$configFile = $_SERVER['SERVER_NAME'] . '.php';
if ($configFile == '.php') $configFile = 'main.php';
$configFile = "$baseDir/config/$configFile";
$app = Yii::createWebApplication($configFile);
$app->run();
return array(
   ...
   'components' => array(
      'db' => array(
        'connectionString'            => 'mysql:host=localhost;dbname=mydb',
        'username'                    => 'some_user',
        'password'                    => 'some_password',
      )
   )
);
// 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'
         )
      )
   )
); 
因此,基本上这会为您提供一个基于服务器主机的自定义配置文件。 显然,如果您使用它,您应该验证主机名以及配置是否存在

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

基本原则不变:
“最终”配置文件包括公共配置,删除不需要的内容(如果适用),并定义一个只包含需要更改的内容的数组结构。此结构与公共配置合并,形成最终配置。

我们正将此扩展用于多服务器网站,它运行得非常好。我们有四个环境,我们在这里和那里做了一些更改,但总的来说,它运行得很好。这实际上就是Yi2中配置的实现方式。少了很多努力。只需合并index.php中的所有配置即可完成,因此可以轻松地进行后端口。