TAAT Technologie Cyfrowe

Zend Framework Tutorial

Config file

We want to store configuration settings of our application in config file in .ini format. Furthermore, we want to have two separate configurations: one for development time (virtual host on local machine), and one for production site. Both configurations have different debugging settings, use different databases and so on.

We want to easily switch between those two configurations. The best way is to have just one switch point: in Apache. At the beginning of our /www/.htaccess we set Apache environment variable defining which config to use:

SetEnv DEVELOPMENT true

Our config file, saved in /application/config/config.ini may look like this:

[global]

; global project settings
project.name = My project
timezone = Europe/Warsaw

; development settings
[development : global]
debug = on
project.name = "My project (development)"

; live settings
[live : global]
debug = off
project.name = "My project (live)"
		

As you can see, there are three sections:

[global]
settings for both live and development sites
[development : global]
settings for development site
[live : global]
settings for live site

Then, in /application/Bootstrap.php we add setConfigmethod to read configuration from file:

/**
 * Read config file
 * @access public
 * @param string $configfile Relative path to config file
 * @param bool $debug null = detect from ENV
 * @return null
 */
 public static function setupConfig($configfile = '/application/config/config.ini') {
        $configSection = strtolower(
                                                        in_array(
                                                                getenv('DEVELOPMENT'), array('on', 'true', 1))
                                                        ) ? 'development' : 'live';
        $config = new Zend_Config_Ini(dirname(dirname(__FILE__)) . $configfile, $configSection);
        Zend_Registry::set('config', $config);
        date_default_timezone_set($config->timezone);
        if ($config->debug) {
                ini_set('display_errors', true);
                error_reporting(E_ALL | E_STRICT);
        } else {
                ini_set('display_errors', false);
                error_reporting(0);
        }
}

Then, we may remove unnesessary code from setEnvironment method, so now it looks like this:

/**
 * setup environment
 * @access public static
 * @return null
 */
public static function setupEnvironment()
{
        self::$root = dirname(dirname(__FILE__));
        set_include_path(
                                         get_include_path() .
                                         PATH_SEPARATOR . self::$root . '/library/'
                                        );
}

Now, we have to update setup() method adding calls to setupConfig():

/**
 * Prepare Front Controller and View
 * @access public static
 * @return null
 */
public static function setup ()
{
        Zend_Loader::registerAutoload();
        self::setupEnvironment();
        self::setupConfig();
        self::setupFrontController();
        self::setupView();
}

As you probably already noticed, I also moved Zend_Loader::registerAutoload();Zend_Loader::registerAutoload(); from setupFrontController() method to setup() method.

To test if everything works ok, put var_dump($configSection); at the end of setupConfig() method, and then try to change SetEnv DEVELOPMENT true to SetEnv DEVELOPMENT false in /www/.htaccess