views:

557

answers:

3

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

A: 

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.

Vladimir
I honestly see no value in separate index files, let alone seeing it as a best way. Ideally, environment should change (that's hardware/server), while code when moving from dev->testing->staging->production should be all the same.The real difference between environments is intended audience (and as such level of debug/log activity). So SetEnv in .htaccess (or virtual host configuration) should define what realm you are in, and once you have a realm/environment - you load corresponding section from your *single* ini file.
Victor Farazdagi
There is no difference where you define it, the advantage is fast switching prod/dev environment.
Vladimir
+2  A: 

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.

Cez
A: 

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

wimvds