Embracing a framework-driven approach to Web development enhances productivity in many ways, not the least of which is the ability to stay DRY (Don't Repeat Yourself). Eliminating the redundant reuse of code throughout a Web application will greatly improve your ability to effectively test and later refactor code, not to mention locate and diagnose the inevitable bugs that will creep into the project.
Solutions such as the Zend Framework aid in the reduction of code redundancy not only by enforcing tier separation through the adoption of best practices such as the Model-View-Controller design pattern, but also by offering a variety of features that facilitate code reuse within both the logic (controllers) and presentation (views). Eliminating code repetition within the controller is particularly beneficial not only for the aforementioned reasons, but also because it increases the likelihood you'll be able to easily reuse the code in future projects.
The Zend Framework refers to these reusable logical snippets as action helpers, and in this article I'll show you how to create, configure and execute action helpers within your Zend Framework-powered applications.
Creating an Action Helper
I'm currently working on a fairly large project that includes numerous special features for registered users. Therefore, the project's success is going to hinge in part upon the quality of the code I use to manage the user account lifecycle, notably registration, login, and password recovery. In order to eliminate the creation of bogus accounts, the user is required to confirm his e-mail address. I send him a one-time URL that contains a unique code associated with that user's account. When the user clicks on the URL, the website confirms the account associated with that unique code. A typical one-time URL looks something like this:
The same approach is employed when the user has forgotten his password and needs to generate a new one. Because he previously confirmed his e-mail address, we can presumably securely confirm the user's request by using that address to send another one-time URL which when clicked either generates a new temporary password or provides the user with a form which he can use to create a new password. This one-time URL might look something like this:
Both tasks require the website to generate a unique code from within two separate actions, making this task a likely candidate for encapsulation within an action helper. The action helper code is just a typical PHP class, which is otherwise endowed with special features by extending the Zend_Controller_Action_Helper_Abstract class. The following example presents the action helper used to generate unique 32-character strings:
As you can see, an action helper indeed is constructed as a typical PHP class, except for one important detail. You must place the code that you intend to execute when the action helper is called from a controller action within a method named direct(). You're free to include other methods and other attributes within the class, which will be used within the direct() method.
When a new project is generated using the Zend_Tool command-line interface, a directory named helpers will be created within the application/controllers directory. It is intended to serve as the location for your action helpers. However, because you probably will reuse action helpers within multiple projects, I recommend instead using a different location that will allow you to group your action helpers with other reusable code. I prefer to use the library directory, which is intended to contain third-party libraries. To change the default location all you need to do is add the following line to your application.ini file:
Note how this configuration directive also defines the class prefix, which in my example is WJG_Controller_Action_Helper.
Executing an Action Helper
With the action helper created and configured, you can begin using it within your controller actions. The action helpers will be attached to the Zend Framework's helper broker:
$confirmationKey = $this->_helper->generateID();
As demonstrated in this article, action helpers can go a long way towards reducing the amount of code that must be redundantly placed throughout an application, allowing you to easily update and refactor this code as needed throughout the application's lifecycle. Perhaps in the future the Zend Framework community will work towards producing a universal action helper repository akin to PEAR!