CHAPTER 17 ■ THE ZEND FRAMEWORK APPLIED^263
Listing 17-4. A Stats Logging Plug-in (YourPrefix_Controller_Plugin_Statistics)
class YourPrefix_Controller_Plugin_Statistics extends
Zend_Controller_Plugin_Abstract {
public function routeStartup($request) {
$uri = $request->getRequestUri();
$dbconfig = array(
'host' => 'localhost',
'username' => 'user',
'password' => 'password',
'dbname' => 'demo'
);
$db = Zend_Db::factory('PDO_PGSQL', $dbconfig);
$data = array(
'uri' => $uri
);
$db->insert('stats', $data);
}
}
To register the plug-in, you need to call registerPlugin() on the front controller:
$front->registerPlugin(new YourPrefix_Controller_Plugin_Statistics());
By using plug-ins, you can collect all sorts of information about your applications in a site-
wide manner.
Creating Helpers
Helpers are classes that are designed to augment and integrate functionality for MVC compo-
nents. The two main types of helpers are action helpers and view helpers.
Writing Action Helpers
Referring back to Figure 17-1, you will notice two more events that are called on the action
HelperBroker: notifyPreDispatch() and notifyPostDispatch(). These two methods are trans-
lated to simply preDispatch() and postDispatch() for your action helpers and can give you the
ability to work with action controller-specific events without registering a site-wide plug-in.
This can enable load-on-use scenarios, which will be much more efficient if you have a large
number of helpers.
The view renderer is a good example of built-in functionality that is deployed as an action
helper. It uses the Zend_Request::getActionName() method as well as the postDispatch() event
to determine and render a template specifically designed for an action.
McArthur_819-9.book Page 263 Friday, February 29, 2008 8:03 AM