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