The front controller has several accessors for setting up its environment. However, there are three primary methods key to the front controller's functionality:
getInstance() is used to retrieve a front
controller instance. As the front controller implements a
Singleton pattern, this is also the only means possible for
instantiating a front controller object.
$front = Zend_Controller_Front::getInstance();
As some examples:
// Set the default controller directory:
// Set several module directories at once:
'default' => '../application/controllers',
'blog' => '../modules/blog/controllers',
'news' => '../modules/news/controllers',
// Add a 'foo' module directory:
If you use
addControllerDirectory() without a
module name, it will set the directory for the
default module -- overwriting it if it already
You can get the current settings for the controller directory
getControllerDirectory(); this will return an
array of module and directory pairs.
One aspect of the front controller is that you may define a modular directory structure for creating standalone components; these are called "modules".
Each module should be in its own directory and mirror the
directory structure of the default module -- i.e., it should
/controllers/ subdirectory at the minimum, and typically
/views/ subdirectory and other application subdirectories.
addModuleDirectory() allows you to pass the name of
a directory containing one or more module directories. It then
scans it and adds them as controller directories to the front
Later, if you want to determine the path to a particular module
or the current module, you can call
getModuleDirectory(), optionally passing a module
name to get that specific module directory.
dispatch(Zend_Controller_Request_Abstract $request = null,
Zend_Controller_Response_Abstract $response = null)
does the heavy work of the front controller. It may optionally
take a request
object and/or a response object,
allowing the developer to pass in custom objects for each.
If no request or response object are passed in,
dispatch() will check for previously registered
objects and use those or instantiate default versions to use in
its process (in both cases, the HTTP flavor will be used as the
The dispatch process has three distinct events:
Routing takes place exactly once, using the values in the
request object when
dispatch() is called.
Dispatching takes place in a loop; a request may either indicate
multiple actions to dispatch, or the controller or a plugin may
reset the request object to force additional actions to
dispatch. When all is done, the front controller returns a
Zend_Controller_Front::run($path) is a static
method taking simply a path to a directory containing
controllers. It fetches a front controller instance (via
registers the path provided via setControllerDirectory(),
and finally dispatches.
run() is a convenience method that can
be used for site setups that do not require customization of the
front controller environment.
// Instantiate front controller, set controller directory, and dispatch in one
// easy step: