jump to navigation

Zend Framework : The Zend_Config component April 8, 2011

Posted by Tournas Dimitrios in Zend Framework v1.10.

The Zend Framework includes a component , Zend_Config , which provides an API for reading and writing configuration files in various formats  (INI , Json , Xml , Yaml , array ) . This component is designed to simplify the access to, and the use of, configuration data within applications. It provides a nested object property-based user interface for accessing this configuration data within application code. The configuration data may  support hierarchical and/or inherited  structure  (INI files) . The best way to illustrate how this component works is with a couple examples .

Reading an INI Configuration file :

The data can be read into a Zend_Config object , and individual configuration variables can then be accessed as object properties using object notation . Lets pretend that our Zend’s central configuration file ( /application/configs/application.ini ) contains the following section :

webhost                  = www.example.com
database.adapter         = pdo_mysql
database.params.host     = db.example.com
database.params.username = dbuser
database.params.password = secret
database.params.dbname   = dbname

Accessing values from any point of our Zend application can be done as follows :

$config = new Zend_Config_Ini($path,'production');
echo $config->database->params->dbname ; //returns dbname

Caution : By default , configuration data represented in a Zend_Config object is read_only , and any attempt to modify it will produce an exception . To allow modifications , pass an additional third argument as true .

Reading an xml Configuration file :
Another adapter adapter of the Zend_Config component is designed to handle XML-encoded configuration data . Lets pretend that we have an xml-encoded file in /application/configs/testconfig.xml .

<?xml version="1.0"?>
                  <adapter value="pdo_mysql"/>
            <host value="db.example.com"/>
            <username value="dbuser"/>
             <password value="secret"/>
             <dbname value="dbname"/>

Accessing values from any point of our Zend application can be done as follows :

$config = new Zend_Config_Xml($path, 'production');
echo $config->database->params->dbname;  //returns dbname


No comments yet — be the first.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s