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
to set or retrieve user parameters.
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.
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
getQuery() for retrieving
$_GET elements, and
retrieving request headers.
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.
Retrieving the Raw POST Data
As of 1.5.0, you can also retrieve the raw post data via the
getRawBody() method. This method returns
FALSE if no data was submitted in that fashion, but the
full body of the post otherwise.
This is primarily useful for accepting content when developing a RESTful MVC application.
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.
getParam() Retrieves More than User Parameters
In order to do some of its work,
retrieves from several sources. In order of priority, these
include: user parameters set via
GET parameters, and finally
parameters. Be aware of this when pulling data via this
If you wish to pull only from parameters you set via
setParam(), use the
Additionally, as of 1.5.0, you can lock down which parameter
sources will be searched.
allows you to specify an empty array or an array with one or
more of the values '_GET' or '_POST' indicating which
parameter sources are allowed (by default, both are
allowed); if you wish to restrict access to only '_GET'
If you are using Apache's 404 handler to pass incoming
requests to the front controller, or using a PT flag with
contains the URI you need, not
$_SERVER['REQUEST_URI']. If you are using such
a setup and getting invalid routing, you should use the
Zend_Controller_Request_Apache404 class instead
of the default HTTP class for your request object:
$request = new Zend_Controller_Request_Apache404();
This class extends the
Zend_Controller_Request_Http class and simply
modifies the autodiscovery of the request URI. It can be
used as a drop-in replacement.
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
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
which will proxy it into the request object. Example usage to
set a custom base URL:
* Dispatch Request with custom base URL with Zend_Controller_Front.
$router = new Zend_Controller_Router_Rewrite();
$controller = Zend_Controller_Front::getInstance();
->setBaseUrl('/projects/myapp'); // set the base url!
$response = $controller->dispatch();
getMethod() allows you to determine the
HTTP request method used to request the current resource.
Additionally, a variety of methods exist that allow you to get
boolean responses when asking if a specific type of request has
The primary use case for these is for creating RESTful MVC architectures.
Zend_Controller_Request_Http has a rudimentary
method for detecting AJAX requests:
isXmlHttpRequest(). This method looks for an
HTTP request header X-Requested-With with
the value 'XMLHttpRequest'; if found, it returns
Currently, this header is known to be passed by default with the following JS libraries:
Prototype and Scriptaculous (and libraries derived from Prototype)
Yahoo! UI Library
Most AJAX libraries allow you to send custom
HTTP request headers; if your library does not send this header,
simply add it as a request header to ensure the
isXmlHttpRequest() method works for you.