# Plugins

## 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 calls on the router to evaluate the request against the registered routes.

- routeShutdown() is called after the router finishes routing the request.

- dispatchLoopStartup() is called before Zend_Controller_Front enters its dispatch loop.

- preDispatch() is called before an action is dispatched by the 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 and/or replaced.

- postDispatch() is called after an action is dispatched by the 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 after Zend_Controller_Front exits its dispatch loop.

## Writing Plugins

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

    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 may implement only those methods required by 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.

## Using Plugins

Plugin classes are registered with Zend_Controller_Front::registerPlugin(), and may be registered at any time. The following snippet illustrates how a plugin may be used in the controller chain:

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

        public function routeShutdown(Zend_Controller_Request_Abstract $request)
        {
            $this->getResponse()
                 ->appendBody("<p>routeShutdown() called</p>\n");
        }

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

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

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

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

    $front = Zend_Controller_Front::getInstance();
    $front->setControllerDirectory('/path/to/controllers')
          ->setRouter(new Zend_Controller_Router_Rewrite())
          ->registerPlugin(new MyPlugin());
    $front->dispatch();

Assuming that no actions called emit any output, and only one action is called, the functionality of the above plugin would still create the following output:

    <p>routeStartup() called</p>
    <p>routeShutdown() called</p>
    <p>dispatchLoopStartup() called</p>
    <p>preDispatch() called</p>
    <p>postDispatch() called</p>
    <p>dispatchLoopShutdown() called</p>

Plugins may be registered at any time during the front controller execution. However, if an event has passed for which the plugin has a registered event method, that method will not be triggered.

## Retrieving and Manipulating Plugins

On occasion, you may need to unregister or retrieve a plugin. The following methods of the front controller allow you to do so:

- getPlugin($class) allows you to retrieve a plugin by class name. If no plugins match, it returns `FALSE`. If more than one plugin of that class is registered, it returns an array.

- getPlugins() retrieves the entire plugin stack.

- unregisterPlugin($plugin) allows you to remove a plugin from the stack. You may pass a plugin object, or the class name of the plugin you wish to unregister. If you pass the class name, any plugins of that class will be removed.

## Plugins Included in the Standard Distribution

Zend Framework includes a plugin for error handling in its standard distribution.