Step-by-Step Pack

Zend Framework Config Step by Step Tutorial

Handle application configuration in Zend Framework using XML, INI, and PHP arrays. Learn to manage database credentials, environment settings, and config inheritance.

Zend Framework configuration management

Why Configuration Matters

Every application needs settings that change between environments. Database credentials are different on your laptop and the production server. Debug mode should be on locally and off in production. API keys vary. Hardcoding these values is a path to broken deployments and accidental credential exposure.

Zend Framework provides Zend_Config to solve this cleanly. It reads configuration from XML, INI, or PHP array files and gives you a simple object interface to access values.

Pack Overview

The main article in this pack covers XML-based configuration. The supporting articles establish the project skeleton and folder structure you need before configuration makes sense. Read the basic tutorial first if you do not have a running Zend Framework project yet.

Walkthrough

Step 1 - Create the Configuration File

Zend Framework supports multiple config formats. XML is the most common for Zend 1.x projects. Create a file at application/configs/application.xml:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<?xml version="1.0"?>
<config>
<production>
<database>
<host>localhost</host>
<username>dbuser</username>
<password>secret</password>
<dbname>myapp</dbname>
</database>
<debug>0</debug>
</production>
<development extends="production">
<database>
<password>devpassword</password>
</database>
<debug>1</debug>
</development>
</config>

The extends attribute is powerful. The development section inherits everything from production and only overrides what is different. This eliminates duplication.

Step 2 - Load the Config in Your Bootstrap

In your bootstrap file or index.php, load the configuration:

1
2
3
4
$config = new Zend_Config_Xml(
APPLICATION_PATH . '/configs/application.xml',
'development'
);

The second parameter selects which section to load. You can set this based on an environment variable so the same code works everywhere.

Step 3 - Access Config Values

Zend_Config objects use property access:

1
2
$host = $config->database->host;
$debug = $config->debug;

No array syntax, no string keys to mistype. If a key does not exist, it returns null instead of throwing a notice.

Step 4 - Store Config in the Registry

To access config values throughout your application, store the config object in Zend_Registry:

1
Zend_Registry::set('config', $config);

Then in any controller or model:

1
2
$config = Zend_Registry::get('config');
$dbname = $config->database->dbname;

This avoids passing the config object through every constructor.

INI Format Alternative

If you prefer INI files over XML:

1
2
3
4
5
6
7
8
9
10
[production]
database.host = localhost
database.username = dbuser
database.password = secret
database.dbname = myapp
debug = 0

[development : production]
database.password = devpassword
debug = 1

Load it with Zend_Config_Ini instead of Zend_Config_Xml. The dot notation in INI files creates nested objects automatically. Both formats produce the same Zend_Config object.

Common Pitfalls

Leaving config files in the public directory. Config files with database passwords must be outside the web root. Place them in application/configs/ and verify that your .htaccess or server config blocks direct access.

Forgetting the section parameter. If you load the XML without specifying a section, you get the entire file as one config tree. That includes all environments mixed together. Always specify the active section.

Not using extends. Duplicating all values in every section leads to errors when you update one section but forget the other. Use inheritance to keep things DRY.

Read-only by default. Zend_Config objects are read-only. If you need to modify values at runtime, pass true as the third parameter to allow modifications. But think twice before doing this - mutable config is usually a design smell.

FAQ

Can I mix XML and INI configs?
Yes. You can load multiple config objects and merge them using Zend_Config::merge(). This is useful when different parts of the application own different config files.

How do I set the environment automatically?
Set an environment variable in Apache with SetEnv APPLICATION_ENV development or in your shell profile. Read it in index.php with getenv('APPLICATION_ENV').

Should I version control my config files?
Version control a template (like application.xml.dist) but not the real file with credentials. Add the real config to .gitignore.

Is there a performance cost to parsing XML on every request?
Minimal, but you can cache the parsed config object using Zend_Cache if it concerns you. Parse once, cache the result, and load from cache on subsequent requests.

What about PHP array configs?
Zend_Config_Array (or just returning an array from a PHP file) is the fastest option since PHP does not need to parse XML or INI. Some developers prefer it for simplicity.

Articles in This Pack

  1. 1 Zend Framework Config - Using XML File Configuration
  2. 2 Zend Framework Basic Tutorial
  3. 3 Zend Framework Intro - Folder Structure

Pack Checklist

  • Zend Framework installed with Zend_Config component available
  • A working Zend Framework project skeleton
  • Basic understanding of XML or INI file syntax
  • PHP 5.2 or later
  • A text editor for creating config files