September 1, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

Ten Tips for Getting the Most from the Zend Framework

  • September 14, 2010
  • By Jason Gilmore
  • Send Email »
  • More Articles »

To truly make your framework-driven websites hum you need to master your chosen framework's nuances, tricks and techniques. Unfortunately, uncovering these finer points often comes only after having suffered through a few poorly implemented projects. In this article I'll try to save relative newcomers to the Zend Framework some pain by identifying 10 key features that have become an indispensable part of my daily development activities.

1. Configure Your Projects with ZF_Tool

The Zend Framework removes many design decisions you'll need to make in terms of how your project is structured. It takes care of defining specific locations and file-naming conventions for the project's models, views, controllers, images and other ancillary files such as CSS and JavaScript. While you can manually create this structure and various MVC files, the Zend Framework offers a command-line utility known as Zend_Tool for not only generating a new project's directory structure, including the Bootstrap file and front controller, but also creating new controllers, actions, models and modules throughout the project. For instance, you can use the following four commands to create a new Zend Framework project, complete with a controller named Account containing the actions login, register and logout:

%>zf create project dev.example.com %>zf create controller Account %>zf create action register Account %>zf create action login Account %>zf create action logout Account

Zend_Tool isn't yet feature complete, but its progress is ongoing and it's already certainly a very capable feature. To learn more about how to use Zend_Tool, see the article Introducing the Zend Framework's Application Configuration Component.

2. DRY Your Logic with Action Helpers

Following the convention of not repeating yourself (commonly referred to as staying DRY) is a hallmark of programming no matter the type of project. The Zend Framework reduces the need to replicate code in many ways, but one feature in particular stands out as an indispensable tool in the quest for efficiency. Known as an action helper, it allows you to consolidate snippets of logic within a special class method and then call that method as needed throughout your application. For instance, suppose you needed to generate a series of random strings at various times within your website. You would use these strings as passwords, password recovery keys, and registration confirmation one-time URLs. Although the code for doing so isn't particularly complex, you wouldn't want to repetitively embed it within each controller action each time you needed it.

Instead, you can consolidate within a single location, bundling it as an action helper, and then call that action helper whenever needed. Although the steps involved in creating an action helper are out of the scope of this article, keep in mind that an action helper is just a standard PHP class that is endowed with special capabilities by extending the Zend_Controller_Action_Helper_Abstract class. When created and configured, you could call the action helper within your actions like this:

$confirmationKey = $this->_helper->generateID();

3. DRY Your Presentations with View Helpers

While the theoretical goal of the Model-View-Controller architecture is to prevent logic from intermingling with an application's presentation, in practice a complete separation of the two is just not possible. However, you can use the Zend Framework's view helper feature to significantly minimize the inconveniences of comingling logic and presentation. For instance, when retrieving result sets from a database you'll often need to refer to the number of records found in either singular or plural format, such as:

11 games added in the past 24 hours!

Because these records are dynamically retrieved, how do you know whether one game or multiple games have been added? The answer is important because it will affect the grammatical structure of the notification. You can make this determination dynamically using a view helper without sacrificing the clean separation of logic and presentation. Like an action helper, a view helper is just a standard PHP class which extends the Zend_View_Helper_Abstract class. We might call the aforementioned view helper Pluralizer, and it would contain code that looks like this:

public function Pluralizer($value, $singleText, $pluralText) { if ($value > 1) { return "{$value} {$pluralText}"; } else { return "{$value} {$singleText}"; } }

With the view helper created and configured, you can call it within your views like this:

&ltp> $count = count($gamesAdded) <?= $this->Pluralizer($count, "game", "games"); ?> added in the past 24 hours! </p>

See the view helper documentation for more information about creating custom view helpers.

4. Test with Zend_Test

One of the most frustrating -- not to mention boring -- Web development tasks is forms testing. Ensuring that a form is properly rendered and that it correctly validates user input, provides appropriate user feedback, and processes the user data can require tremendous amounts of time. Also, you'll often need to return to each task time and again as your application evolves.

Embracing a test-driven development process can almost completely remove this tedium and frustration! The Zend Framework's Zend_Test component, which integrates with the popular PHPUnit testing framework, allows you to write tests that can automate the verification of all of the aforementioned form characteristics! However, Zend_Test's capabilities go far beyond merely testing forms; you can write tests that verify the proper operation of models, controller and action existence, Web services integration, and much more.

5. Streamline Notifications with the Flash Messenger

Providing your users with a streamlined interface will play a crucial role in their overall satisfaction. Part of this streamlining has to do with reducing the number of pages a user must navigate. One easy way to reduce this number is by using the Zend Framework's FlashMessenger action helper to provide notification messages to the user on the next request. For instance, you could automatically redirect a user to his profile page after successfully logging into his account and on that profile page display a special one-time message acknowledging the successful login. For instance, the following message will add a message to the flash messenger message array and then redirect the user to the Account controller's profile action:

$this->_helper->flashMessenger->addMessage('You have successfully logged in'); $this->_helper->redirector('profile', 'account');

You can retrieve these messages within the next request's action using the following snippet:

if ($this->_helper->FlashMessenger->hasMessages()) { $this->view->messages = $this->_helper->FlashMessenger->getMessages(); }

Tags: PHP, Zend



Page 1 of 2



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Sitemap | Contact Us

Rocket Fuel