zend框架自动开关生产阶段测试..等

发布于 2024-08-26 14:23:06 字数 294 浏览 14 评论 0原文

我需要更改什么才能从生产切换到登台.. 等等.. 以及在哪里.. Bootstrap ?

另外,好奇是否有人将他们的 Zend Framework 配置为自动从 生产、登台、测试..等基于主机信息..

示例..

 if (hostname = 'prodServer') ... blah
 if (hostname = 'testServer') ... blah

我是 Zend 的新手,但我通常将我的项目配置为自动切换 基于主机信息的运行环境。

谢谢

What do I change to switch from production to staging.. etc.. and where.. Bootstrap ?

Also, Curious if anyone has configured their Zend Framework to automatically switch from
production, staging, test.. etc based on Host information..

example..

 if (hostname = 'prodServer') ... blah
 if (hostname = 'testServer') ... blah

I'm new to Zend but I typically configure my projects to automatically switch
run environments based on the host information.

thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

人│生佛魔见 2024-09-02 14:23:06

假设您使用 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 生成项目,则由 Zend_Tool 添加它。

Assuming that you are using APPLICATION_ENV as part of Zend_Application, then you could add this in either your .htaccess or main Apache config (assuming Apache is in use - should still be possible with different Web servers too).

For example, in your .htaccess/config (assumes 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

Then ensure that APPLICATION_ENV is set in index.php by using:

// Define application environment
defined('APPLICATION_ENV') || define('APPLICATION_ENV', (getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV') : 'production'));

This is added by Zend_Tool if you use it to generate the project.

千と千尋 2024-09-02 14:23:06

这对我来说在 .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"

That work for me in .htaccess

SetEnvIf Host dev.mydomain.ca APPLICATION_ENV=development
SetEnvIf Host mydomain.ca APPLICATION_ENV=production
SetEnvIf Host mydomain.localhost APPLICATION_ENV=production

Then in my 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"
阿楠 2024-09-02 14:23:06

我们定义了一个环境变量(ENVPHP),并在我们的XML配置文件中使用它,因此只要定义正确的ENVPHP环境变量,就会加载正确的数据库参数。使用 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() 来检查我所处的环境并根据它执行我想要的代码。

顺便说一句,ENVPHP 环境变量可以使用 ie 在 apache 配置文件中设置。 VirtualHost 容器中的 SetEnv ENVPHP "dev"。对于 CLI PHP 脚本,您应该将其设置为操作系统环境变量...

We define an environment variable (ENVPHP), and use this in our XML configuration files, so the correct DB parameters are loaded as long as you define the correct ENVPHP environment variable. Using XML you can extend (or override) your common parameters with those for the specific environments.

ie. the configuration looks as follows :

<?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>

And to load the configuration, I have the following in my bootstrap (well, actually in an Application singleton class) :

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;
}

In PHP code if I want to do some things only for specific environments then I use Application::getEnv() to check what environment I'm in and execute the code I want according to it.

BTW The ENVPHP environment variable can be set in your apache configuration file using ie. SetEnv ENVPHP "dev" within your VirtualHost container. For CLI PHP scripts you should set it as an OS environment variable...

蹲在坟头点根烟 2024-09-02 14:23:06

我看到的最好的方法是:

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

但第一种方法要好得多。

The best way that I saw is:

index.php - production
index_dev.php - dev, index_dev.php/controller/action

I also tried host named configs files:

base.ini - base config
localhost.ini - dev config
prod.host.com.ini - prod config

but the first approach is much better.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文