14.4. Controllers

A controller responds to requests and provides logic that determines the models and views used to form the response. Put controller classes in the module folder, in the controllers folder.

Here is the skeleton of a controller for the Foo module:

 * Controller description
 * @copyright   copyright info
 * @license     license info
 * @version     version info
class Foo_IndexController extends Zend_Controller_Action
    public $contexts = array(
        'index' => array('json'),

     * Description
    public function indexAction()

14.4.1. View Scripts

By default, each action method in a controller needs a corresponding view script to display the results of the action. Each view script contains HTML and, optionally, PHP directives. View scripts are organized by controller using the following folder structure:


In the preceding structure, <controller> refers to the name of the controller and <action> refers to the name of the action. For example, the path to the view script for our skeleton controller is: views/scripts/index/index.phtml.

14.4.2. Request Context

The action can be invoked for a full-page request or an AJAX request, where the desired output is JSON, XML, or partial HTML (if a specific chunk of markup forms only part of an existing page).

Perforce Chronicle enhances the use of controllers by providing a simple way to declare contexts. Note the $contexts declaration at the top of the skeleton controller above. It specifies an associative array with action method names as keys, and an array of possible alternative contexts the action may respond with. Valid values include:

  • partial: indicates partial HTML is available, with no layout markup.
  • json: indicates JavaScript Object Notation (JSON) is available.
  • xml: indicates that XML output is available.
  • dojoio: indicates that output tailored for dojo.io.iframe requests is available.

Additionally, the contexts specified can be restricted to specific HTTP methods. For example:

public $contexts = array(
    'foo'   => array('partial' => 'get', 'json' => 'post'),
    'bar'   => array('dojoio'  => 'get', 'xml'  => 'put'),
    'baz'   => array('json')

For the "foo" action, the preceding declaration permits partial HTML responses for GET requests and JSON responses for POST requests. For the "bar" action, it permits DojoIO or XML responses to GET or PUT requests respectively. Lastly for the "baz" action, JSON responses are supported for all request methods.

Unless an action specifically disables layouts or other view rendering, the default output is standard full-page HTML. Each context needs its own correspondingly-named view script, using the following format:


For details, refer to the Zend Framework documentation.

Perforce Chronicle - Release: 2012.2/486814