Zend Framework Quick Start

Create a Configuration and Registry

Application configurations are an important part of creating modular and configurable applications. This is where Zend_Config comes into play. First we will create an application configuration file and put it inside application/config/app.ini.

;; application/config/app.ini
;; 
;; This is a sample app.ini file.  Your application will dictate the format and the
;; type of sections and data that can be found in this ini file.  It will also dictate
;; how many ini files will be contained in your config/ directory.  For the puropose
;; of our application, this one file makes the most sense.

;; We always have our "production" section first, because it will define ALL of the 
;; keys that our application is expecting to see, and reduce deployment issues
;; resulting from configuration.

[production]

[development : production]

[testing : production]

Zend_Config has several adapters for parsing configuration files; each returns an object with which you can then access the values in the configuration using an object-oriented interface. In this case, we will use Zend_Config_Ini.

Now, what if we want to access this configuration object elsewhere in our application? The standard design pattern used in such situations is the Registry. Zend_Registry is Zend Framework's implementation of this pattern.

In this next code sample, we will instruct our bootstrap file to load the proper section of the config file and add it to our application registry.

// application/bootstrap.php
//
// CONFIGURATION - Setup the configuration object
// The Zend_Config_Ini component will parse the ini file, and resolve all of
// the values for the given section.  Here we will be using the section name
// that corresponds to the APP's Environment
$configuration = new Zend_Config_Ini(
    APPLICATION_PATH . '/config/app.ini', 
    APPLICATION_ENVIRONMENT
);

// REGISTRY - setup the application registry
// An application registry allows the application to store application 
// necessary objects into a safe and consistent (non global) place for future 
// retrieval.  This allows the application to ensure that regardless of what 
// happends in the global scope, the registry will contain the objects it 
// needs.
$registry = Zend_Registry::getInstance();
$registry->configuration = $configuration;
$registry->dbAdapter     = $dbAdapter;

// CLEANUP - remove items from global scope
// This will clear all our local boostrap variables from the global scope of 
// this script (and any scripts that called bootstrap).  This will enforce 
// object retrieval through the Applications's Registry
unset($frontController, $view, $configuration, $registry);
Why use the registry instead of globals?

Usage of globals is often considered an anti-pattern. Globals are difficult to test against, as you cannot predict where and when they may be overwritten. A registry can help prevent this, and also ensure that there is a single location for retrieving objects.



Create a Layout
Create a Model and Database Table

Quickstart Downloads

Grab the QuickStart code for reference, or to start your project:

ZF Reference Guide - now in PDF!

The Zend Framework Reference Guide is now available in PDF format from Zend's high-speed content distribution network. Registration is required.

Documentation Archives

If you're looking for an older version of our reference guide, you'll find it in our download archives.