jump to navigation

Zend Framework : How Plugins fit in Zend’s lifecycle May 11, 2011

Posted by Tournas Dimitrios in Zend Framework v1.10.
trackback

In Zend Framework, plugins are used to listen for certain events in the front controller’s  life cycle and provides an easy way to extend the functionality and behavior of your entire web application  . The controller architecture includes a plugin system that allows user code to be called when certain events occur in front controller’s process life-cycle . You can create your own plugin by extending abstract class Zend_Controller_Plugin_Abstract , this Class provides the following methods that you can ether override all or some :

  • routeStartup : This method is called before Zend_Controller_Front calls  the router . Router routes request to specific module/controller/action
  • routeShutdown : This method is called when the router finishes his routing  job
  • dispatchLoopStartup : This method is called before Zend_Controller_Front enter dispatch loop startup
  • predispatch : This method is called before action is called by the dispatcher
  • postdispatch : This method is called after action is dispatched by the dispatcher
  • DispatchShutdown : This method is called before Zend_Controller_Front exit dispatch loop .

There are many uses for Front Controller plugins , you should consider using them when you need to affect the application as a whole such as initialization . If you need to affect a subset of your application , such as a module , then you would be better to use an Action Helper

Exploring Zend’s applications life-cycle by creating  a custom Plugin :

First create a “Plugins” directory  in project’s  ” lib ” directory .
Now create a simple PHP  file in the “Plugins” directory with the name of  Myplugin.php ( the name is up to you ) .

<?php
      class MyPlugin extends Zend_Controller_Plugin_Abstract
  
      { 
 public function routeStartup(Zend_Controller_Request_Abstract $request) 
          {
  
  echo "<p><h2>routeStartup() called</h2></p>" ;
  $this->getResponse()->appendBody("<p>routeStartup() called</p>\n");
          }       
   
   public function routeShutdown(Zend_Controller_Request_Abstract $request)
          { 
 
 echo "<p><h2>routeShutdown() called</h2></p>" ;
 $this->getResponse()->appendBody("<p>routeShutdown() called</p>\n");
          } 
		  
 public function dispatchLoopStartup(Zend_Controller_Request_Abstract $request)  
          {  

  echo "<p><h2>dispatchLoopStartup() called</h2></p>" ;
  $this->getResponse()->appendBody("<p>dispatchLoopStartup() called</p>\n");
          }  
		  
   public function preDispatch(Zend_Controller_Request_Abstract $request)  
          {    
  echo "<p><h2>preDispatch() called</h2></p>" ;
  $this->getResponse()->appendBody("<p>preDispatch() called</p>\n");
          }  
		  
  public function postDispatch(Zend_Controller_Request_Abstract $request)  
          {  
  echo "<p><h2>postDispatch() called</h2></p>" ;
  $this->getResponse()->appendBody("<p>postDispatch() called</p>\n");
          }    
 
 public function dispatchLoopShutdown()
 {
 echo "<p><h2>dispatchLoopShutdown() called</h2></p>" ; 
$this->getResponse()->appendBody("<p>dispatchLoopShutdown() called</p>\n");
 
 } 
      }       
  
      

Let’s now register this plugin in our bootstrap file ( index.php) :

require_once 'MyPlugins/MyPlugin.php';
$front = Zend_Controller_Front::getInstance();
$front->registerPlugin(new MyPlugin());

Run your application and watch it’s life-cycle , we can clearly discover the stages that our application goes through . Assuming that our index action controller is empty and ” index view” only contains a Header-text  , the functionality of the above plugin would create the following output :

Zend Framework includes by default  an error handler Plugin and an ViewRenderer Plugin  in its standard distribution.

A nice practical example where we have intruded in Zend’s life-cycle  is when an application  implements layout , the layout template is rendered before any output produced by the view script . Another possible practical example would be when some resources have been assigned permissions ( Acl ) , so before using these resources , a plugin will check if the user has right to access them .

Links :

Advertisements

Comments»

No comments yet — be the first.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s