Using Zend_Config

There is a Zend Framework class to help in configuring your application. You can use it to save and retrieve configuration data. It’s called Zend_Config . For examples on how to use it see http://framework.zend.com/manual/en/zend.config.html.

It allows you to use an ‘.ini’ or an xml file to load your configuration settings. You can then access them as you would of any objects property. Thus if you have this line

include.path = 'a/path/'

in your ini file, after using Zend_Config_Ini, you would access it in this manner

$path = $config->include->path;

The process would be similar if you had used xml instead and Zend_Config_Xml.
While being really handy, this method still presents some challenges. An example seaks louder thhan words:

includes.APP_MODELS_DIR = MY_APP.DS.'models';

where MY_APP and DS are constants defined in my boostrap file, let’s say.
The
parse_ini_file() php function, used by Zend_Config_Ini does’nt resolve MY_APP and DS to their proper values. And don’t even try to use such a syntax

includes.APP_MODELS_DIR = <?php MODEL_DIR.DIRECTORY_SEPARATOR."somestring";?>

otherwise you would get an error. I mean it’s quite understandable. The parse_ini_file() function is expecting ini syntax and nothing else.
Since I use constants extensively when defining my paths (app,libs,controllers,vendors paths for example), I ‘ld rather create an ‘include’ object. For example I would have an ‘include.php’ file:

$includes = null;
$includes->MODELS_DIR = MY_APP.DS.'models';
$includes->APP_MODELS_DIR = MY_APP.DS.'models';
$includes->APP_CLASSES_DIR = MY_APP.DS.'classes';
$includes->PLUGINS_DIR = MY_LIBS.DS.'ZendPlugins';
$includes->VIEW_PATH = MY_APP.DS.'views'.DS.'scripts';

where I can use previously defined constants (they are defined in the bootstrap file as a necessity for the bootstrap to happen at all).
Then I write a utility function to
parse the object into an ‘include string’ that I append to my include path:

function makeIncludePath($includes)
{
$include_path='';
if($includes)
{
foreach($includes as $inc)
{$include_path.=$inc. DS.PATH_SEPARATOR;}
}
ini_set('include_path', ini_get('include_path') . DS.PATH_SEPARATOR.$include_path);
}


In a suitable place, before the dispatch process has started, I write

require_once 'config_utils.php';
require_once 'includes.php';
makeIncludePath($includes);

I realise that by using my own object I lose the benefit of all the convenient methods defined in Zend_Conf_Ini, but for such a simple job, I might not feel the difference. Yet I am thinking about extending Zend_Conf_Ini with the method discussed above. I might be worth it. Find out more on the subject here:
Using ZEND_Config Part 2

Explore posts in the same categories: Zend Framework

Tags: ,

You can comment below, or link to this permanent URL from your own site.

Comment: