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

Subclassing the Dispatcher

Zend_Controller_Front will first call the router to determine the first action in the request. It then enters a dispatch loop, which calls on the dispatcher to dispatch the action.

The dispatcher needs a variety of data in order to do its work - it needs to know how to format controller and action names, where to look for controller class files, whether or not a provided module name is valid, and an API for determining if a given request is even dispatchable based on the other information available.

Zend_Controller_Dispatcher_Interface defines the following methods as required for any dispatcher implementation:

<?php
interface Zend_Controller_Dispatcher_Interface
{
    
/**
     * Format a string into a controller class name.
     *
     * @param string $unformatted
     * @return string
     */
    
public function formatControllerName($unformatted);

    
/**
     * Format a string into an action method name.
     *
     * @param string $unformatted
     * @return string
     */
    
public function formatActionName($unformatted);

    
/**
     * Determine if a request is dispatchable
     *
     * @param  Zend_Controller_Request_Abstract $request
     * @return boolean
     */
    
public function isDispatchable(
        
Zend_Controller_Request_Abstract $request
    
);

    
/**
     * Set a user parameter (via front controller, or for local use)
     *
     * @param string $name
     * @param mixed $value
     * @return Zend_Controller_Dispatcher_Interface
     */
    
public function setParam($name$value);

    
/**
     * Set an array of user parameters
     *
     * @param array $params
     * @return Zend_Controller_Dispatcher_Interface
     */
    
public function setParams(array $params);

    
/**
     * Retrieve a single user parameter
     *
     * @param string $name
     * @return mixed
     */
    
public function getParam($name);

    
/**
     * Retrieve all user parameters
     *
     * @return array
     */
    
public function getParams();

    
/**
     * Clear the user parameter stack, or a single user parameter
     *
     * @param null|string|array single key or array of keys for
     *        params to clear
     * @return Zend_Controller_Dispatcher_Interface
     */
    
public function clearParams($name null);

    
/**
     * Set the response object to use, if any
     *
     * @param Zend_Controller_Response_Abstract|null $response
     * @return void
     */
    
public function setResponse(
        
Zend_Controller_Response_Abstract $response null
    
);

    
/**
     * Retrieve the response object, if any
     *
     * @return Zend_Controller_Response_Abstract|null
     */
    
public function getResponse();

    
/**
     * Add a controller directory to the controller directory stack
     *
     * @param string $path
     * @param string $args
     * @return Zend_Controller_Dispatcher_Interface
     */
    
public function addControllerDirectory($path$args null);

    
/**
     * Set the directory (or directories) where controller files are
     * stored
     *
     * @param string|array $dir
     * @return Zend_Controller_Dispatcher_Interface
     */
    
public function setControllerDirectory($path);

    
/**
     * Return the currently set directory(ies) for controller file
     * lookup
     *
     * @return array
     */
    
public function getControllerDirectory();

    
/**
     * Dispatch a request to a (module/)controller/action.
     *
     * @param  Zend_Controller_Request_Abstract $request
     * @param  Zend_Controller_Response_Abstract $response
     * @return Zend_Controller_Request_Abstract|boolean
     */
    
public function dispatch(
        
Zend_Controller_Request_Abstract $request,
        
Zend_Controller_Response_Abstract $response
    
);

    
/**
     * Whether or not a given module is valid
     *
     * @param string $module
     * @return boolean
     */
    
public function isValidModule($module);

    
/**
     * Retrieve the default module name
     *
     * @return string
     */
    
public function getDefaultModule();

    
/**
     * Retrieve the default controller name
     *
     * @return string
     */
    
public function getDefaultControllerName();

    
/**
     * Retrieve the default action
     *
     * @return string
     */
    
public function getDefaultAction();
}

In most cases, however, you should simply extend the abstract class Zend_Controller_Dispatcher_Abstract, in which each of these have already been defined, or Zend_Controller_Dispatcher_Standard to modify functionality of the standard dispatcher.

Possible reasons to subclass the dispatcher include a desire to use a different class or method naming schema in your action controllers, or a desire to use a different dispatching paradigm such as dispatching to action files under controller directories (instead of dispatching to class methods).

Zend Framework