The Standard Router

The Dispatcher

Overview

Dispatching is the process of taking the request object, Zend_Controller_Request_Abstract, extracting the module name, controller name, action name, and optional parameters contained in it, and then instantiating a controller and calling an action of that controller. If any of the module, controller, or action are not found, it will use default values for them. Zend_Controller_Dispatcher_Standard specifies index for each of the controller and action defaults and default for the module default value, but allows the developer to change the default values for each using the setDefaultController(), setDefaultAction(), and setDefaultModule() methods, respectively.

Note: Default Module
When creating modular applications, you may find that you want your default module namespaced as well (the default configuration is that the default module is not namespaced). As of 1.5.0, you can now do so by specifying the prefixDefaultModule as TRUE in either the front controller or your dispatcher:

  1. // In your front controller:
  2. $front->setParam('prefixDefaultModule'// In your dispatcher:
  3. $dispatcher->setParam('prefixDefaultModule'
This allows you to re-purpose an existing module to be the default module for an application.

Dispatching happens in a loop in the front controller. Before dispatching occurs, the front controller routes the request to find user specified values for the module, controller, action, and optional parameters. It then enters a dispatch loop, dispatching the request.

At the beginning of each iteration, it sets a flag in the request object indicating that the action has been dispatched. If an action or pre or postDispatch plugin resets that flag, the dispatch loop will continue and attempt to dispatch the new request. By changing the controller and/or action in the request and resetting the dispatched flag, the developer may define a chain of requests to perform.

The action controller method that controls such dispatching is _forward(); call this method from any of the preDispatch(), postDispatch() or action methods, providing an action, controller, module, and optionally any additional parameters you may wish to send to the new action:

  1. span style="color: #808080; font-style: italic;">// forward to another action in the current controller and module:
  2. 'bar''baz' => 'bogus'// forward to an action in another controller:
  3.     // FooController::bazAction(),
  4.     // in the current module:
  5. 'baz', 'foo''baz' => 'bogus'// forward to an action in another controller in another module,
  6.     // Foo_BarController::bazAction():
  7. 'baz', 'bar', 'foo''baz' => 'bogus'));
  8. }

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:

  1. span style="color: #808080; font-style: italic;">/**
  2.      * Format a string into a controller class name.
  3.      *
  4.      * @param string $unformatted
  5.      * @return string
  6.      *//**
  7.      * Format a string into an action method name.
  8.      *
  9.      * @param string $unformatted
  10.      * @return string
  11.      *//**
  12.      * Determine if a request is dispatchable
  13.      *
  14.      * @param  Zend_Controller_Request_Abstract $request
  15.      * @return boolean
  16.      *//**
  17.      * Set a user parameter (via front controller, or for local use)
  18.      *
  19.      * @param string $name
  20.      * @param mixed $value
  21.      * @return Zend_Controller_Dispatcher_Interface
  22.      *//**
  23.      * Set an array of user parameters
  24.      *
  25.      * @param array $params
  26.      * @return Zend_Controller_Dispatcher_Interface
  27.      *//**
  28.      * Retrieve a single user parameter
  29.      *
  30.      * @param string $name
  31.      * @return mixed
  32.      *//**
  33.      * Retrieve all user parameters
  34.      *
  35.      * @return array
  36.      *//**
  37.      * Clear the user parameter stack, or a single user parameter
  38.      *
  39.      * @param null|string|array single key or array of keys for
  40.      *        params to clear
  41.      * @return Zend_Controller_Dispatcher_Interface
  42.      *//**
  43.      * Set the response object to use, if any
  44.      *
  45.      * @param Zend_Controller_Response_Abstract|null $response
  46.      * @return void
  47.      *//**
  48.      * Retrieve the response object, if any
  49.      *
  50.      * @return Zend_Controller_Response_Abstract|null
  51.      *//**
  52.      * Add a controller directory to the controller directory stack
  53.      *
  54.      * @param string $path
  55.      * @param string $args
  56.      * @return Zend_Controller_Dispatcher_Interface
  57.      *//**
  58.      * Set the directory (or directories) where controller files are
  59.      * stored
  60.      *
  61.      * @param string|array $dir
  62.      * @return Zend_Controller_Dispatcher_Interface
  63.      *//**
  64.      * Return the currently set directory(ies) for controller file
  65.      * lookup
  66.      *
  67.      * @return array
  68.      *//**
  69.      * Dispatch a request to a (module/)controller/action.
  70.      *
  71.      * @param  Zend_Controller_Request_Abstract $request
  72.      * @param  Zend_Controller_Response_Abstract $response
  73.      * @return Zend_Controller_Request_Abstract|boolean
  74.      *//**
  75.      * Whether or not a given module is valid
  76.      *
  77.      * @param string $module
  78.      * @return boolean
  79.      *//**
  80.      * Retrieve the default module name
  81.      *
  82.      * @return string
  83.      *//**
  84.      * Retrieve the default controller name
  85.      *
  86.      * @return string
  87.      *//**
  88.      * Retrieve the default action
  89.      *
  90.      * @return string
  91.      */

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).


The Standard Router