7.4. The Request Object

7.4.1. Introduction

The request object is a simple value object that is passed between Zend_Controller_Front and the router, dispatcher, and controller classes. It packages the names of the requested module, controller, action, and optional parameters, as well as the rest of the request environment, be it HTTP, the CLI, or PHP-GTK.

  • The module name is accessed by getModuleName() and setModuleName().

  • The controller name is accessed by getControllerName() and setControllerName().

  • The name of the action to call within that controller is accessed by getActionName() and setActionName().

  • Parameters to be accesible by the action are an associative array of key/value pairs that are retrieved by getParams() and set with setParams(), or individually by getParam() and setParam().

Based on the type of request, there may be more methods available. The default request used, Zend_Controller_Request_Http, for instance, has methods for retrieving the request URI, path information, $_GET and $_POST parameters, etc.

The request object is passed to the front controller, or if none is provided, it is instantiated at the beginning of the dispatch process, before routing occurs. It is passed through to every object in the dispatch chain.

Additionally, the request object is particularly useful in testing. The developer may craft the request environment, including module, controller, action, parameters, URI, etc, and pass the request object to the front controller to test application flow. When paired with the response object, elaborate and precise unit testing of MVC applications becomes possible.

7.4.2. HTTP Requests

7.4.2.1. Accessing Request Data

Zend_Controller_Request_Http encapsulates access to relevant values such as the key name and value for the controller and action router variables, and all additional parameters parsed from the URI. It additionally allows access to values contained in the superglobals as public members, and manages the current Base URL and Request URI. Superglobal values cannot be set on a request object, instead use the setParam/getParam methods to set or retrieve user parameters.

[Opmerking] Superglobal data

When accessing superglobal data through Zend_Controller_Request_Http as public member properties, it is necessary to keep in mind that the property name (superglobal array key) is matched to a superglobal in a specific order of precedence: 1. GET, 2. POST, 3. COOKIE, 4. SERVER, 5. ENV.

Specific superglobals can be accessed using a public method as an alternative. For example, the raw value of $_POST['user'] can be accessed by calling getPost('user') on the request object. These include getQuery() for retrieving $_GET elements, and getHeader() for retrieving request headers.

[Opmerking] GET and POST data

Be cautious when accessing data from the request object as it is not filtered in any way. The router and dispatcher validate and filter data for use with their tasks, but leave the data untouched in the request object.

You may also set user parameters in the request object using setParam() and retrieve these later using getParam(). The router makes use of this functionality to set parameters matched in the request URI into the request object.

[Opmerking] getParam() retrieves more than user params

In order to do some of its work, getParam() actually retrieves from several sources. In order of priority, these include: user parameters set via setParam(), GET parameters, POST parameters, and finally COOKIE parameters. Be aware of this when pulling data via this method.

7.4.2.2. Base Url and Subdirectories

Zend_Controller_Request_Http allows Zend_Controller_Router_Rewrite to be used in subdirectories. Zend_Controller_Request_Http will attempt to automatically detect your base URL and set it accordingly.

For example, if you keep your index.php in a webserver subdirectory named /projects/myapp/index.php, base URL (rewrite base) should be set to /projects/myapp. This string will then be stripped from the beginning of the path before calculating any route matches. This frees one from the necessity of prepending it to any of your routes. A route of 'user/:username' will match URIs like http://localhost/projects/myapp/user/martel and http://example.com/user/martel.

[Opmerking] URL detection is case sensitive

Automatic base URL detection is case sensitive, so make sure your URL will match a subdirectory name in a filesystem (even on Windows machines). If it doesn't, an exception will be raised.

Should base URL be detected incorrectly you can override it with your own base path with the help of the setBaseUrl() method of either the Zend_Controller_Request_Http class, or the Zend_Controller_Front class. The easiest method is to set it in Zend_Controller_Front, which will proxy it into the request object. Example usage to set a custom base URL:

<?php
/**
 * Dispatch Request with custom base URL with Zend_Controller_Front.
 */
$router     = new Zend_Controller_Router_Rewrite();
$controller = Zend_Controller_Front::getInstance();
$controller->setControllerDirectory('./application/controllers')
           ->setRouter($router)
           ->setBaseUrl('/projects/myapp'); // set the base url!
$response   = $controller->dispatch();
?>

7.4.3. Subclassing the Request Object

The base request class used for all request objects is the abstract class Zend_Controller_Request_Abstract. At its most basic, it defines the following methods:

abstract class Zend_Controller_Request_Abstract
{
    /**
     * @return string
     */
    public function getControllerName();

    /**
     * @param string $value 
     * @return self
     */
    public function setControllerName($value);

    /**
     * @return string
     */
    public function getActionName();

    /**
     * @param string $value 
     * @return self
     */
    public function setActionName($value);

    /**
     * @return string
     */
    public function getControllerKey();

    /**
     * @param string $key 
     * @return self
     */
    public function setControllerKey($key);

    /**
     * @return string
     */
    public function getActionKey();

    /**
     * @param string $key 
     * @return self
     */
    public function setActionKey($key);

    /**
     * @param string $key 
     * @return mixed
     */
    public function getParam($key);

    /**
     * @param string $key 
     * @param mixed $value 
     * @return self
     */
    public function setParam($key, $value);

    /**
     * @return array
     */
     public function getParams();

    /**
     * @param array $array 
     * @return self
     */
    public function setParams(array $array);

    /**
     * @param boolean $flag 
     * @return self
     */
    public function setDispatched($flag = true);

    /**
     * @return boolean
     */
    public function isDispatched();
}

The request object is a container for the request environment. The controller chain really only needs to know how to set and retrieve the controller, action, optional parameters, and dispatched status. By default, the request will search its own parameters using the controller or action keys in order to determine the controller and action.

Extend this class, or one of its derivatives, when you need the request class to interact with a specific environment in order to retrieve data for use in the above tasks. Examples include the HTTP environment, a CLI environment, or a PHP-GTK environment.