PhpRiot
Become Zend Certified

Prepare for the ZCE exam using our quizzes (web or iPad/iPhone). More info...


When you're ready get 7.5% off your exam voucher using voucher CJQNOV23 at the Zend Store

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:

<?php
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:

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

Note

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.

Zend Framework