Goals of the plugins

zend-frameworkThe most important is understand Zend Framework architecture. We have controllers, views, models and now we going to introduce a new concept, the plugin. The plugin is a code snippet that is executed in our application for each request

 Let's see how it works:



As you can see, the plugin always executes before controller and this is a big advantage because you can perform any global task before a specific action start. Typical plugin tasks are:

  • Authoritation and authentication level.
  • Modules level, such as navigation bar.
  • Routing
  • Module loading


Keys of plugins

  • The plugin "always" executes before controller
  • Is executed for each request.


Note: Also, is allowed execute the code after controller. There are several methods.

Default methods:

  • routeStartup
  • routeShutdown
  • dispatchLoopStartup
  • preDispatch
  • postDispatch
  • dispatchLoopShutdown


Plugin structure and example

Let's code!

Is very important to create right file structure.

File path: "library/App/Plugin/Acl.php"


 * Plugin example
class App_Plugin_Acl extends Zend_Controller_Plugin_Abstract {
    protected $_acl;
public function __construct() { $this->_acl = new Zend_Acl(); } public function preDispatch(Zend_Controller_Request_Abstract $request) { // do something foo(true); } public funcion foo($param) { // do something } public function postDispatch(Zend_Controller_Request_Abstract $request) { // do something bar(false); } public funcion bar($param) { // do something } ?>



 Futhermore, you will need to include this lines in your bootstrap file:


class Bootstrap extends Zend_Application_Bootstrap_Bootstrap {
    protected function _initAutoload() {
        // Autoloaders
        $autoloader = Zend_Loader_Autoloader::getInstance();
        // Plugin register
        $front = Zend_Controller_Front::getInstance();
        $front->registerPlugin(new App_Plugin_Acl());


Now, for each request, the defaults methods will run in correct order