如何切换生产环境和测试环境,需要在哪里进行修改?使用Bootstrap吗?
还想知道是否有人已经配置了他们的Zend框架,根据主机信息自动切换生产、测试、预发布等环境。
例如..
if (hostname = 'prodServer') ... blah
if (hostname = 'testServer') ... blah
我对Zend不熟悉,但通常会根据主机信息自动切换运行环境来配置我的项目。
谢谢。
如何切换生产环境和测试环境,需要在哪里进行修改?使用Bootstrap吗?
还想知道是否有人已经配置了他们的Zend框架,根据主机信息自动切换生产、测试、预发布等环境。
例如..
if (hostname = 'prodServer') ... blah
if (hostname = 'testServer') ... blah
我对Zend不熟悉,但通常会根据主机信息自动切换运行环境来配置我的项目。
谢谢。
假设你正在使用Zend_Application中的APPLICATION_ENV,那么你可以在你的.htaccess或主要的Apache配置文件中添加这个(假设正在使用Apache - 在不同的Web服务器上也应该仍然可行)。
例如,在你的.htaccess/config中(假设启用了mod_setenv):
SetEnvIf HTTP_HOST abc.example.com APPLICATION_ENV=production
SetEnvIf HTTP_HOST def.example.com APPLICATION_ENV=staging
SetEnvIf HTTP_HOST ghi.example.com APPLICATION_ENV=development
然后通过以下方式确保在index.php中设置了APPLICATION_ENV:
// Define application environment
defined('APPLICATION_ENV') || define('APPLICATION_ENV', (getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV') : 'production'));
如果您使用Zend_Tool生成项目,则会添加此内容。
以下是我在 .htaccess 中使用的方法
SetEnvIf Host dev.mydomain.ca APPLICATION_ENV=development
SetEnvIf Host mydomain.ca APPLICATION_ENV=production
SetEnvIf Host mydomain.localhost APPLICATION_ENV=production
接下来在我的application.ini文件中
[development : production]
phpSettings.display_startup_errors = 1
phpSettings.display_errors = 1
resources.frontController.params.displayExceptions = 1
; Database for development
resources.db.params.dbname = "mydabase-dev"
<?xml version="1.0" encoding="UTF-8"?>
<application>
<common>
<name>MyApp_name</name>
<code>MyApp_code</code>
<version>MyApp_version</version>
<authentication>
... authentication specific parameters (ie. LDAP connection parameters)
</authentication>
...
</common>
<dev extends="common">
<database>
... DB connection parameters for development
</database>
...
</dev>
<tst extends="common">
<database>
... DB connection parameters for test
</database>
...
</tst>
<prd extends="common">
<database>
... DB connection parameters for production
</database>
...
</prd>
</application>
为了加载配置,我在我的引导程序中(实际上是在一个应用程序单例类中)有以下内容:
public static function getEnv()
{
if (self::$env === null) {
self::$env = getenv('ENVPHP');
} else {
return self::$env;
}
}
protected function initConfig ()
{
$configFile = $this->appDir . '/config/application.xml';
if (! is_readable($configFile)) {
throw new Application_Exception('Config file "' . $configFile . '" is not readable');
}
if (false === self::getEnv()) {
throw new Application_Exception('The environment variable "ENVPHP" is not defined');
}
$config = new Zend_Config_Xml($configFile, self::getEnv(), true);
$config->setReadOnly();
Zend_Registry::set('config', $config);
$this->config = $config;
}
SetEnv ENVPHP "dev"
。对于 CLI PHP 脚本,你应该将其设置为操作系统环境变量...我看到的最好的方法是:
index.php - production
index_dev.php - dev, index_dev.php/controller/action
我也尝试了以主机命名的配置文件:
base.ini - base config
localhost.ini - dev config
prod.host.com.ini - prod config
但第一种方法要好得多。