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

Primary Methods

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

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.

<?php
$front 
Zend_Controller_Front::getInstance();

setControllerDirectory() and addControllerDirectory

setControllerDirectory() is used to tell the dispatcher where to look for action controller class files. It accepts either a single path or an associative array of module and path pairs.

As some examples:

<?php
// Set the default controller directory:
$front->setControllerDirectory('../application/controllers');

// Set several module directories at once:
$front->setControllerDirectory(array(
    
'default' => '../application/controllers',
    
'blog'    => '../modules/blog/controllers',
    
'news'    => '../modules/news/controllers',
));

// Add a 'foo' module directory:
$front->addControllerDirectory('../modules/foo/controllers''foo');

Note

If you use addControllerDirectory() without a module name, it will set the directory for the default module -- overwriting it if it already exists.

You can get the current settings for the controller directory using getControllerDirectory(); this will return an array of module and directory pairs.

addModuleDirectory() and getModuleDirectory()

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 have a /controllers/ subdirectory at the minimum, and typically a /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 controller.

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

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

Similarly, dispatch() checks for registered router and dispatcher objects, instantiating the default versions of each if none is found.

The dispatch process has three distinct events:

  • Routing

  • Dispatching

  • Response

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

run()

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 getInstance()), registers the path provided via setControllerDirectory(), and finally dispatches.

Basically, run() is a convenience method that can be used for site setups that do not require customization of the front controller environment.

<?php
// Instantiate front controller, set controller directory, and dispatch in one
// easy step:
Zend_Controller_Front::run('../application/controllers');

Zend Framework