zend framework auto switch production staging test .. etc
Asked Answered
S

4

6

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

Sudatory answered 24/3, 2010 at 18:52 Comment(0)
D
16

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.

Disturb answered 26/3, 2010 at 9:0 Comment(2)
SetEnvIf Host def.example.com APPLICATION_ENV=staging works for meFreightage
better way seems to be: SetEnvIf HOST ^abc.example.com$ APPLICATION_ENV=production ...Sirrah
I
4

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"
Implacental answered 30/6, 2011 at 20:15 Comment(0)
B
1

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

Bat answered 26/3, 2010 at 11:57 Comment(0)
D
0

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.

Deviant answered 26/3, 2010 at 8:26 Comment(3)
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.Newfeld
There is no difference where you define it, the advantage is fast switching prod/dev environment.Deviant
The difference is, that in the one case you define the environment in the server and in the other case, the client will choose the environment. This has several disadvantages: Security, if you deploy a index_dev.php. Maintenance: you have two or more points in the code, where your env is defined (config, index_<env>.php, api_<env>.php, cli_<api>.php, ...); You have to tell your clients (AJAX, WebService-Clients, Shell-Scrips, ...), to access different files/URIs for different environments, you even have to tell your clients, what environment "you" want the client to request.Maladapted

© 2022 - 2024 — McMap. All rights reserved.