Zend Framework Plugins

10 Nov

Plugins allow user code to be called when certain events occurs in the controller process life time.

You can create your own plugin by extending abstract class Zend_Controller_Plugin_Abstract.
Zend_Controller_Plugin_Abstract provide following methods that you can either override all or some.

  • routeStartup(Zend_Controller_Request_Abstract $request) :

This method is called before Zend_Controller_Front call router. Router route request to specific module/Controller/Action.

  • routeShutdown(Zend_Controller_Request_Abstract $request):

This method is called when router finishes routing.

  • dispatchLoopStartup(Zend_Controller_Request_Abstract $request):

This method is called before Zend_Controller_Front enter dispatch loop startup.

  • preDispatch(Zend_Controller_Request_Abstract $request)

This method is called before action is called by the dispatcher.

  • postDispatch(Zend_Controller_Request_Abstract $request)

This method is called after action is dispatched by the dispatcher.

  • DispatchLoopShutdown():

This method is called before Zend_Controller_Front exit dispatch loop.
Creating Plugin:
Before writing the code, create My/Controller/Plugin/ directory structure under library/.
Create a file Simple.php in this directory and place the following code in it.


<?php
class My_Controller_Plugin_Simple extends Zend_Controller_Plugin_Abstract
{
public function routeStartup(Zend_Controller_Request_Abstract $request)
{
echo ‘router start up called’;
}
public function routeShutdown(Zend_Controller_Request_Abstract $request)
{
echo ‘router shutdown called’;
}
public function dispatchLoopStartup(Zend_Controller_Request_Abstract $request)
{
echo ‘dispatch loop started.’;
}
public function preDispatch(Zend_Controller_Request_Abstract $request)
{
echo ‘pre dispatch called’;
}
public function postDispatch(Zend_Controller_Request_Abstract $request)
{
echo ‘post dispatch called’;
}
public function dispatchLoopShutdown()
{
echo ‘dispatch loop ended.’;
}

}
?>



As you have now created your own plugin, you will need to register it with your front controller.
Add the following code to your bootstrap file for registering the plugin.

$frontController->registerPlugin(new My_Controller_Plugin_Simple);


These methods will be called every time you request your action.

One Response to “Zend Framework Plugins”

  1. Darkhan July 10, 2009 at 7:59 am #

    so, simple )

Leave a comment