Skip to end of metadata
Go to start of metadata

<p>Zend_Config allows a web application to store basic configuration values. If you like, you can utilize a separate staging environment for testing new development and a production environment for publishing development that has been successfully tested in the staging environment.</p>

<ac:macro ac:name="code"><ac:parameter ac:name="title">config.ini</ac:parameter><ac:plain-text-body><![CDATA[
; Staging environment
[staging]
host = staging.example.com
db.type = pdo_mysql
db.host = localhost
db.username = someuser
db.password = somepass
db.name = somedb;

Production environment inherits values from staging environment and
; overrides values that are specific to the production environment
[production : staging]
host = www.example.com
db.host = db.example.com
db.username = anotheruser
db.password = anotherpass
]]></ac:plain-text-body></ac:macro>

<p>In order to load the configuration values for the staging environment, a developer using Zend_Config would write:</p>
<ac:macro ac:name="code"><ac:parameter ac:name="title">config.php</ac:parameter><ac:plain-text-body><![CDATA[
<?php
require_once 'Zend/Config/Ini.php';

$config = new Zend_Config_Ini('/path/to/config.ini', 'staging');

echo $config->host; // prints "staging.www.example.com"
echo $config->db->host; // prints "localhost"
?>
]]></ac:plain-text-body></ac:macro>

<p>Zend_Config also provides loaders for configuration data stored in PHP arrays and in XML-formatted text files, and developers are free to implement their own loaders for other formats where desirable.</p>

<h3>Using Zend_Config_Xml</h3>

<p>It is also possible to use xml for providing configuration info. Following the example above, your structure and files should look like this to achieve the same result. Note that the root node doesn't necessarily have to be called 'configdata'.</p>

<ac:macro ac:name="code"><ac:default-parameter>xml</ac:default-parameter><ac:plain-text-body><![CDATA[
<?xml version="1.0"?>
<configdata>
<staging>
<host>staging.example.com</host>
<dbtype>pdo_mysql</dbtype>
<dbhost>localhost</dbhost>
<dbuser>someuser</dbuser>
<dbpass>somepass</dbpass>
<dbname>somedb</dbname>
</staging>
<production extends="staging">
<host>www.example.com</host>
<dbhost>db.example.com</dbhost>
<dbuser>anotheruser</dbuser>
<dbpass>anotherpass</dbpass>
</production >
</configdata>
]]></ac:plain-text-body></ac:macro>

<ac:macro ac:name="code"><ac:plain-text-body><![CDATA[
<?php
$config = new Zend_Config_Xml('config.xml', 'staging', true);
?>
]]></ac:plain-text-body></ac:macro>

Labels:
zend_config zend_config Delete
example example Delete
Enter labels to add to this page:
Please wait 
Looking for a label? Just start typing.