我要从生产切换到登台..等等..以及在哪里..引导程序?
此外,好奇是否有人将其 Zend 框架配置为根据主机信息自动从生产、登台、测试等切换。
例子..
if (hostname = 'prodServer') ... blah
if (hostname = 'testServer') ... blah
我是 Zend 的新手,但我通常将我的项目配置为根据主机信息自动切换运行环境。
谢谢
我要从生产切换到登台..等等..以及在哪里..引导程序?
此外,好奇是否有人将其 Zend 框架配置为根据主机信息自动从生产、登台、测试等切换。
例子..
if (hostname = 'prodServer') ... blah
if (hostname = 'testServer') ... blah
我是 Zend 的新手,但我通常将我的项目配置为根据主机信息自动切换运行环境。
谢谢
假设您使用 APPLICATION_ENV 作为 Zend_Application 的一部分,那么您可以将它添加到您的 .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"
我们定义了一个环境变量 (ENVPHP),并在我们的 XML 配置文件中使用它,因此只要您定义了正确的 ENVPHP 环境变量,就会加载正确的 DB 参数。使用 XML,您可以使用特定环境的参数扩展(或覆盖)常用参数。
IE。配置如下:
<?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;
}
在 PHP 代码中,如果我只想为特定环境做一些事情,那么我使用 Application::getEnv() 检查我所处的环境并根据它执行我想要的代码。
顺便说一句,可以使用 ie 在您的 apache 配置文件中设置 ENVPHP 环境变量。SetEnv ENVPHP "dev"
在您的 VirtualHost 容器中。对于 CLI PHP 脚本,您应该将其设置为操作系统环境变量...
我看到的最好的方法是:
index.php - production
index_dev.php - dev, index_dev.php/controller/action
我还尝试了名为 configs 文件的主机:
base.ini - base config
localhost.ini - dev config
prod.host.com.ini - prod config
但第一种方法要好得多。