Introducing the Zend_Config Component
The Zend_Config component provides a
standardized solution for managing and accessing your
application's configuration data. This configuration data is
most commonly managed within files using INI and XML syntax
formats, however it's also possible to access configuration
data stored within an array, meaning you could just as
easily manage the data within a database and populate the
configuration array as necessary. Because the INI file is
not only easy to understand but also seems to be the most
widely used within Zend Framework applications, I'll use
that as the basis for the examples throughout this tutorial,
although applying what you learn to the other available
formats should be straightforward.
Managing Your Configuration Data
Presuming you use Zend_Tool to create your
Zend Framework projects, in your project directory you'll
find a directory named configs. This is the default location
for storing configuration data related to your project.
Within the configs directory you'll find a file named
application.ini which contains several default
configuration settings. The original file looks like this:
[production]
phpSettings.display_startup_errors = 0
phpSettings.display_errors = 0
includePaths.library = APPLICATION_PATH "/../library"
bootstrap.path = APPLICATION_PATH "/Bootstrap.php"
bootstrap.class = "Bootstrap"
resources.frontController.controllerDirectory = APPLICATION_PATH "/controllers"
[staging : production]
[testing : production]
phpSettings.display_startup_errors = 1
phpSettings.display_errors = 1
[development : production]
phpSettings.display_startup_errors = 1
phpSettings.display_errors = 1
As you can see, a dotted naming convention is used. Some
of the naming prefixes such as phpSettings and
bootstrap are of special significance to the
Zend Framework. For instance, using the
phpSettings prefix allows you to assign an
application-specific value to one of PHP's configuration
directives. In the above listing you'll see the
display_errors directive has been set to 0,
meaning any errors generated during the application's
execution will not be displayed to the browser. For
organizational purposes you are free to create your own
prefixes. For instance when organizing the e-mail addresses
used throughout a Web site I use the logical prefix
email:
email.support = "support@example.com"
email.admin = "admin@example.com"
email.sales = "sales@example.com"
You probably also noticed certain configuration variables
are repeated throughout the configuration file. This is
because the Zend_Config component allows you to
manage stage-specific configuration variables. For instance,
you'll in all likelihood use several databases in
conjunction with your application; one for development,
another for staging, and still another for production. To
accommodate the various stages, you'll see the configuration
file is separated into sections titled production, staging,
testing, and development. By default the staging, testing,
and development sections inherit from the production
section, as indicated by the colon which denotes
inheritance. By taking advantage of this feature, you won't
have to worry about redundantly listing configuration
variables whose values do not change as the application is
migrated from one stage to another.
But how does the application know which set of
configuration variables to use? The stage can be configured
in a number of ways, however the most commonplace is within
the .htaccess file, located within your
application's public directory. Open .htaccess
and you'll find that a variable named
APPLICATION_ENV has been assigned the value
development, meaning the variables found within the
application.ini's [development] section will be used.
Accessing the Configuration Data
With the configuration data neatly organized within the
application.ini file, we'll want to begin accessing that
data within the application's controllers. To access this
data throughout a controller's actions, add the following
three lines to your controller's init method:
$bootstrap = $this->getInvokeArg('bootstrap');
$configArray = $bootstrap->getOptions();
$this->config = new Zend_Config($configArray);
Now suppose you wanted to access the sales e-mail address
configuration variable I had presented earlier in the
tutorial, which looked like this:
email.sales = "sales@example.com"
The $this->config variable is actually an
object, with all of the configuration variables having been
associated with this object as attributes. Therefore to
retrieve the sales e-mail address, you'll use the following
syntax:
$email = $this->config->email->sales;
Where to From Here?
The Zend Framework's Zend_Config component makes it
trivial to manage your application's configuration data
within a central location. In taking advantage of this great
feature, you'll remove yet another tedious task from your
TODO list, freeing up additional time to think about what
counts the most: creating a great Web application!
Original Article