Die Controller Architektur beinhaltet ein Pluginsystem, das den Aufruf von Anwendercode ermöglicht, wenn bestimmte Ereignisse im Controller Prozess auftreten. Der Front Controller verwendet einen Plugin Broker als eine Registry für User Plugins und der Plugin Broker stellt sicher, dass die Ereignismethoden von jedem Plugin aufgerufen werden, die im Front Controller registriert worden sind.
            Die Ereignismethoden sind in der abstrakten Klasse 
            Zend_Controller_Plugin_Abstract definiert, von dem User Plugin Klassen 
            angeleitet werden müssen:
        
                    routeStartup() wird aufgerufen bevor
                    Zend_Controller_Front den Router
                    aufruft, um den Request anhand der registrierten Routen zu überprüfen.
                
                    routeShutdown() wird aufgerufen, nachdem der Router
                    das Routing der Anfrage beendet.
                
                    dispatchLoopStartup() wird aufgerufen, bevor 
                    Zend_Controller_Front den Dispatch Loop aufnimmt.
                
                    preDispatch() wird von 
                    dem Dispatcher
                    aufgerufen, bevor eine Aktion verarbeitet wird. Dieser Callback erlaubt ein 
                    Proxy oder Filter Verhalten. Durch Verändern des Requests und Zurücksetzen
                    des Verarbeitungsstatus (mittels 
                    Zend_Controller_Request_Abstract::setDispatched(false)) kann
                    die aktuelle Aktion abgebrochen oder ersetzt werden.
                
                    postDispatch() wird von 
                    dem Dispatcher
                    aufgerufen, nachdem eine Aktion verarbeitet wurde. Dieser Callback erlaubt ein 
                    Proxy oder Filter Verhalten. Durch Verändern des Requests und Zurücksetzen
                    des Verarbeitungsstatus (mittels 
                    Zend_Controller_Request_Abstract::setDispatched(false)) kann
                    eine neue Aktion für die Verarbeitung angegeben werden.
                
                    dispatchLoopShutdown() wird aufgerufen, nachdem
                    Zend_Controller_Front den Dispatch Loop beendet.
                
            Um eine Plugin Klasse zu schreiben, bindet man einfach die abstrakte Klasse 
            Zend_Controller_Plugin_Abstract ein und erweitert sie:
        
<?php
require_once 'Zend/Controller/Plugin/Abstract.php';
class MyPlugin extends Zend_Controller_Plugin_Abstract
{
    // ...
}
?>
            Keine der Methoden von Zend_Controller_Plugin_Abstract ist abstrakt, so 
            dass Plugin Klassen nicht gezwungen werden, irgend einen der vorhandenen 
            Ereignismethoden zu implemetieren, die oben gelistet wurden. Schreiber von Plugins 
            brauchen nur die Methoden zu implementieren, die sie für ihre speziellen Bedürfnisse 
            benötigen.
        
            Zend_Controller_Plugin_Abstract stellt den Controller Plugins außerdem die 
            Request und Response Objekte über die getRequest() und 
            getResponse() Methoden zur Verfügung.
        
            Plugin Klassen werden mit Zend_Controller_Front::registerPlugin() 
            registriert und können jederzeit registriert werden. Der folgende Schnipsel zeigt, 
            wie ein Plugin in der Controllerkette verwendet werden kann:
        
<?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>\n");
    }
    public function routeShutdown($request)
    {
        $this->getResponse()->appendBody("<p>routeShutdown() called</p>\n");
    }
    public function dispatchLoopStartup($request)
    {
        $this->getResponse()->appendBody("<p>dispatchLoopStartup() called</p>\n");
    }
    public function preDispatch($request)
    {
        $this->getResponse()->appendBody("<p>preDispatch() called</p>\n");
    }
    public function postDispatch($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>
![]()  | 
Anmerkung | 
|---|---|
| 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. | 
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.
            
Zend Framework includes a plugin for error handling in its standard distribution.
        Zend_Controller_Plugins_ErrorHandler provides a drop-in
        plugin for handling exceptions thrown by your application, including
        those resulting from missing controllers or actions; it is an
        alternative to the methods listed in the MVC Exceptions section.
    
The primary targets of the plugin are:
Intercept exceptions raised due to missing controllers or action methods
Intercept exceptions raised within action controllers
In other words, the ErrorHandler plugin is designed to handle HTTP 404-type errors (page missing) and 500-type errors (internal error). It is not intended to catch exceptions raised in other plugins or routing.
        By default, Zend_Controller_Plugins_ErrorHandler will
        forward to ErrorController::errorAction() in the default
        module. You may set alternate values for these by using the various
        accessors available to the plugin:
    
                setErrorHandlerModule() sets the controller module
                to use.
            
                setErrorHandlerController() sets the controller 
                to use.
            
                setErrorHandlerAction() sets the controller action 
                to use.
            
                setErrorHandler() takes an associative array, which
                may contain any of the keys 'module', 'controller', or 'action',
                with which it will set the appropriate values.
            
        Additionally, you may pass an optional associative array to the
        constructor, which will then proxy to setErrorHandler().
    
        Zend_Controller_Plugin_ErrorHandler registers a
        postDispatch() hook and checks for exceptions registered in
        the response object. If
        any are found, it attempts to forward to the registered error handler
        action.
    
If an exception occurs dispatching the error handler, the plugin will tell the front controller to throw exceptions, and rethrow the last exception registered with the response object.
Since the ErrorHandler plugin captures not only application errors, but also errors in the controller chain arising from missing controller classes and/or action methods, it can be used as a 404 handler. To do so, you will need to have your error controller check the exception type.
            Exceptions captured are logged in an object registered in the
            request. To retrieve it, use
            Zend_Controller_Action::_getParam('error_handler'):
        
<?php
class ErrorController extends Zend_Controller_Action
{
    public function errorAction()
    {
        $errors = $this->_getRequest('error_handler');
    }
}
?>
            Once you have the error object, you can get the type via
            $errors->type. It will be one of the following:
        
                    Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_CONTROLLER,
                    indicating the controller was not found.
                
                    Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_ACTION,
                    indicating the requested action was not found.
                
                    Zend_Controller_Plugin_ErrorHandler::EXCEPTION_OTHER,
                    indicating other exceptions.
                
You can then test for either of the first two types, and, if so, indicate a 404 page:
<?php
class ErrorController extends Zend_Controller_Action
{
    public function errorAction()
    {
        $errors = $this->_getRequest('error_handler');
        switch ($errors->type) {
            case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_CONTROLLER:
            case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_ACTION:
                // 404 error -- controller or action not found
                $this->getResponse()->setRawHeader('HTTP/1.1 404 Not Found');
                // ... get some output to display...
                break;
            default:
                // application error; display error page, but don't change
                // status code
                break;
        }
    }
}
?>
Beispiel 7.11. Standard usage
<?php require_once 'Zend/Controller/Front.php'; require_once 'Zend/Controller/Plugin/ErrorHandler.php'; $front = Zend_Controller_Front::getInstance(); $front->registerPlugin(new Zend_Controller_Plugin_ErrorHandler()); ?>
Beispiel 7.12. Setting a different error handler
<?php
require_once 'Zend/Controller/Front.php';
require_once 'Zend/Controller/Plugin/ErrorHandler.php';
$front = Zend_Controller_Front::getInstance();
$front->registerPlugin(new Zend_Controller_Plugin_ErrorHandler(array
    'module'     => 'mystuff',
    'controller' => 'static',
    'action'     => 'error'
)));
?>
Beispiel 7.13. Using accessors
<?php
require_once 'Zend/Controller/Front.php';
require_once 'Zend/Controller/Plugin/ErrorHandler.php';
$plugin = new Zend_Controller_Plugin_ErrorHandler();
$plugin->setErrorHandlerModule('mystuff')
       ->setErrorHandlerController('static')
       ->setErrorHandlerAction('error');
$front = Zend_Controller_Front::getInstance();
$front->registerPlugin($plugin);
?>
In order to use the Error Handler plugin, you need an error controller. Below is a simple example.
<?php
class ErrorController extends Zend_Controller_Action
{
    public function errorAction()
    {
        $errors = $this->_getRequest('error_handler');
        switch ($errors->type) {
            case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_CONTROLLER:
            case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_ACTION:
                // 404 error -- controller or action not found
                $this->getResponse()->setRawHeader('HTTP/1.1 404 Not Found');
                $content =<<<EOH
<h1>Error!</h1>
<p>The page you requested was not found.</p>
EOH;
                break;
            default:
                // application error
                $content =<<<EOH
<h1>Error!</h1>
<p>An unexpected error occurred with your request. Please try again later.</p>
EOH;
                break;
        }
        $this->initView();
        $this->view->content = $content;
        $this->render();
    }
}
?>