2.5. Plugins
Prev | Chapter 2. Zend_Controller | Next

## 2.5. Plugins

### 2.5.1. Introduction

The controller architecture includes a plugin system that allows user code to be called when certain events occur in the controller process lifetime. The front controller uses a plugin broker as a registry for user plugins, and the plugin broker ensures that event methods are called on each plugin registered with the front controller.

The event methods are defined in the abstract class `Zend_Controller_Plugin_Abstract`, from which user plugin classes inherit:

- `routeStartup()` is called before `Zend_Controller_Front` begins evaluating the request against its routes.
- `routeShutdown()` is called after `Zend_Controller_Router` exits, when Zend_Controller_Front exits from the router.
- `dispatchLoopStartup()` is called before `Zend_Controller_Front` enters its dispatch loop.
- `preDispatch()` is called before an action is dispatched by `Zend_Controller_Dispatcher`. This callback allows for proxy or filter behavior. By altering the request and resetting its dispatched flag (via `Zend_Controller_Request_Abstract::setDispatched(false)`), the current action may be skipped.
- `postDispatch()` is called after an action is dispatched by `Zend_Controller_Dispatcher`. This callback allows for proxy or filter behavior. By altering the request and resetting its dispatched flag (via `Zend_Controller_Request_Abstract::setDispatched(false)`), a new action may be specified for dispatching.
- `dispatchLoopShutdown()` is called before `Zend_Controller_Front` exits its dispatch loop.

### 2.5.2. Writing Plugins

In order to write a plugin class, simply include and extend the abstract class `Zend_Controller_Plugin_Abstract`:

    <?php
    require_once 'Zend/Controller/Plugin/Abstract.php';

    class MyPlugin extends Zend_Controller_Plugin_Abstract
    {
        // ...
    }

None of the methods of `Zend_Controller_Plugin_Abstract` are abstract, and this means that plugin classes are not forced to implement any of the available event methods listed above. Plugin writers can implement whatever even methods may suit their particular needs.

`Zend_Controller_Plugin_Abstract` also makes the request and response objects available to controller plugins via the `getRequest()` and `getResponse()` methods, respectively.

### 2.5.3. Using Plugins

Plugin classes are registered with `Zend_Controller_Front::registerPlugin()` prior to dispatching. The following snippet illustrates how a plugin may be used in the controller chain:

    <?php
    require_once 'Zend/Controller/Front.php';
    require_once 'Zend/Controller/Router.php';
    require_once 'Zend/Controller/Plugin/Abstract.php';

    class MyPlugin extends Zend_Controller_Plugin_Abstract
    {
        public function routeStartup()
        {
            $this->getResponse()->appendBody('<p>routeStartup() called</p>');
        }

        public function routeShutdown($request)
        {
            $this->getResponse()->appendBody('<p>routeShutdown() called</p>');
        }

        public function dispatchLoopStartup($request)
        {
            $this->getResponse()->appendBody('<p>dispatchLoopStartup() called</p>');
        }

        public function preDispatch($request)
        {
            $this->getResponse()->appendBody('<p>preDispatch() called</p>');
        }

        public function postDispatch($request)
        {
            $this->getResponse()->appendBody('<p>postDispatch() called</p>');
        }

        public function dispatchLoopShutdown()
        {
            $this->getResponse()->appendBody('<p>dispatchLoopShutdown() called</p>');
        }
    }

    $controller = new Zend_Controller_Front();
    $controller->setControllerDirectory('/path/to/controllers')
               ->setRouter(new Zend_Controller_Router())
               ->registerPlugin(new MyPlugin());
    $response = $controller->dispatch();

    echo $response;

Prev | Up | Next
2.4. Provided Subclasses | Home | Chapter 3. Zend_Http