【问题标题】:zend framework auto switch production staging test .. etczend框架自动切换生产分期测试..等
【发布时间】:2010-03-24 18:52:05
【问题描述】:

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

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

例子..

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

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

谢谢

【问题讨论】:

    标签: zend-framework environment production staging


    【解决方案1】:

    假设您使用 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
    

    然后确保 APPLICATION_ENV 在 index.php 中使用:

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

    如果您使用 Zend_Tool 生成项目,则会添加它。

    【讨论】:

    • SetEnvIf Host def.example.com APPLICATION_ENV=staging 对我有用
    • 更好的方法似乎是:SetEnvIf HOST ^abc.example.com$ APPLICATION_ENV=production ...
    【解决方案2】:

    在 .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"
    

    【讨论】:

      【解决方案3】:

      我们定义了一个环境变量 (ENVPHP),并在我们的 XML 配置文件中使用它,因此只要您定义了正确的 ENVPHP 环境变量,就会加载正确的 DB 参数。使用 XML,您可以使用特定环境的参数扩展(或覆盖)常用参数。

      即。配置如下:

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

      为了加载配置,我的引导程序中有以下内容(嗯,实际上是在 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 配置文件中设置。 SetEnv ENVPHP "dev" 在您的 VirtualHost 容器中。对于 CLI PHP 脚本,您应该将其设置为操作系统环境变量...

      【讨论】:

        【解决方案4】:

        我看到的最好的方法是:

        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
        

        但第一种方法要好得多。

        【讨论】:

        • 老实说,我认为单独的索引文件没有任何价值,更不用说将其视为最佳方式了。理想情况下,环境应该改变(即硬件/服务器),而从 dev->testing->staging->production 移动时的代码应该都是一样的。环境之间的真正区别在于目标受众(以及调试/日志活动的级别)。所以 .htaccess (或虚拟主机配置)中的 SetEnv 应该定义你所在的领域,一旦你有一个领域/环境 - 你从 single ini 文件中加载相应的部分。
        • 在哪里定义没有区别,优点是快速切换prod/dev环境。
        • 不同之处在于,一种情况是你在服务端定义环境,另一种情况是客户端选择环境。这有几个缺点: 安全性,如果您部署 index_dev.php。维护:您在代码中有两个或多个点,其中定义了您的 env (config, index_.php, api_.php, cli_.php, ...);您必须告诉您的客户(AJAX、WebService-Clients、Shell-Scrips,...),以访问不同环境的不同文件/URI,您甚至必须告诉您的客户,“您”希望客户请求什么环境.
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-08-06
        • 2012-02-25
        • 1970-01-01
        • 2014-04-29
        • 1970-01-01
        • 2018-04-25
        相关资源
        最近更新 更多