January 19, 2021
Hot Topics:

Ten Tips for Getting the Most from the Zend Framework

  • By Jason Gilmore
  • Send Email »
  • More Articles »

6. Maintain Configuration Data Centrally with Zend_Config

Even the simplest of websites contains a great deal of configuration-related data, including database connection parameters, Web service API keys and email addresses. Adding to the complexity of managing this data is the need to often use different sets of configuration data depending upon the current stage of an application's lifecycle. For instance, you hopefully use one database when developing the site and another for the production stage. Using the Zend Framework's Zend_Config component, you can manage this data in a central location and call upon it as needed throughout the application.

The default storage location is within a file named application.ini, which resides in the application/configs directory. This file is organized into various sections, with each representing a stage of the application lifecycle. Here's an abbreviated snippet of what this file might look like:

[production] email.support = "support@example.com"; [development : production] email.support = "bugzilla@example.com";

Notice how the email is defined according to the application lifecycle stage. If the application lifecycle is set to production, the support@example.com email will be retrieved when needed. When set to development, bugzilla@example.com will be retrieved. You can set the application lifecycle within the .htaccess file by setting the APPLICATION_ENV variable like this:

SetEnv APPLICATION_ENV development

Within your controller actions you can then retrieve the email.support variable like this:

$options = new Zend_Config($this->getInvokeArg('bootstrap')->getOptions()); $this->view->supportEmail = $options->support->email;

Read the article Introducing the Zend Framework's Application Configuration Component to learn more about this indispensable feature.

7. Master the Zend_Db Component's Relationships Feature

The Zend_Db component provides a convenient programming interface for the underlying database, which relieves developers from the need to write much of the SQL queries used to mine and manipulate data. For instance, retrieving a user account using the accounts table's underlying integer-based primary key is as simple as passing the key to the associated model's find() method:

$accountModel = new Application_Model_Account(); $account = $accountModel->find(45)->current();

But such conveniences really only scratch the surface in terms of this powerful component's capabilities. Among many other features, you can formally define the parent/child relationship between two models and then programmatically manage those relationships using the Zend_Db component. For instance, suppose you wanted to associate each user account with his state of residence, meaning the Account model would be dependent upon the State model. With the relationship defined, you can retrieve the name of the account's associated state like this:

$accountModel = new Application_Model_Account(); $account = $accountModel->find(45)->current(); $state = $account->findParentRow('Application_Model_State')->name;

It's also possible to find dependent records, for instance all accounts associated with a particular state, using the findDependentRowset() method!

8. Don't Reinvent the Web Services Wheel

It seems that each of the popular frameworks offers a unique characteristic that sets it apart from other solutions. In the Zend Framework's case, that unique feature is the vast Web services support it offers. You can find components capable of interfacing with popular services including Twitter, Google, Amazon, Microsoft Azure and others. So, be sure to take advantage of these components rather than worry about integrating third-party solutions.

9. Leverage the Native Validators

Validating user input, particularly complex strings such as email addresses, ranks among the most tedious and time-consuming tasks of any project. Yet the urgency of properly validating this input means you can't avoid it. The Zend Framework removes much of the complexity by offering a large number of validators through the Zend_Validate component. Among others, you'll find validators for verifying email addresses, IP addresses, URLs, credit card numbers, and much more. Be sure to make ample use of these validators throughout your application!

10. Modify Default Routes

The Zend Framework optimizes URLs for both search engines and users right out of the box, constructing URLs that generally follow the pattern http://www.example.com/controller/action/key1/value1/keyN/valueN/. However, you may wish to provide users with a number of convenience URLs such as http://www.example.com/login. You can override the Zend Framework's default routing features by defining your own custom routes within the Bootstrap.php file. For instance, you can add the aforementioned custom route by adding the following method to your Bootstrap:

public function _initRoutes() { $frontController = Zend_Controller_Front::getInstance(); $router = $frontController->getRouter(); $route = new Zend_Controller_Router_Route_Static( 'login', array('controller' => 'Account', 'action' => 'login') ); $router->addRoute('login', $route); }

See the article Creating Custom Routes with the Zend Framework to learn more about this powerful feature.


Do you have any useful Zend Framework-related tips or tricks in mind? Tell us all about them in the comments!

About the Author

Jason Gilmore is is the founder of the publishing and consulting firm WJGilmore.com. He is the author of several popular books, including "Easy PHP Websites with the Zend Framework", "Easy PayPal with PHP", and "Beginning PHP and MySQL, Third Edition". Follow him on Twitter at @wjgilmore.

Page 2 of 2

This article was originally published on September 15, 2010

Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Thanks for your registration, follow us on our social networks to keep up-to-date