Pro PHP- Patterns, Frameworks, Testing and More

(vip2019) #1
CHAPTER 16 ■ ADVANCED ZEND FRAMEWORK^237

The first argument points to the INI file, and the second argument indicates the section
to use.

The XML Approach.


The XML approach to configuration offers all the benefits of the INI methodology, but may
allow for greater portability between applications. It also comes with the overhead of parsing
an XML file. Listing 16-3 shows an example of an XML configuration file.

Listing 16-3. Configuration Using an XML File (Config.xml)

<?xml version="1.0"?>
<configdata>
<production>
<domain>some.com</domain>
<database>
<name>dbname</name>
<password>password</password>
</database>
</production>
<staging extends="production">
<domain>beta.some.com</domain>
<database>
<name>dbname</name>
</staging>
</configdata>

Accessing the XML configuration data works the same as with the INI file approach:

$config = new Zend_Config_Xml('config.xml', 'staging');
echo $config->database->password;

password

I don’t recommend using XML for configuration data, as the overhead of parsing an XML
document is actually quite high. That said, you can use caching to neutralize this concern, as
discussed in the “Caching” section later in this chapter.

Setting Site-Wide View Variables


By default, the framework will create a new Zend_View instance and register it with the view
renderer for you automatically. However, in some circumstances, you may wish to customize
the view. Customization of the view instance will allow you to set default application-wide
variables, change the default path structure, and set various options. Listing 16-4 shows an example
of registering a custom view.

McArthur_819-9C16.fm Page 237 Friday, February 29, 2008 5:07 PM

Free download pdf