A free and open-source book on ZF3 for beginners


4.4. Retrieving Data from HTTP Request

In a controller's action method, you may need to retrieve the data from the HTTP request (the data like GET and POST variables, cookies, HTTP headers and so on). For this purpose, Zend Framework 3 provides you with Zend\Http\Request class, which is part of Zend\Http component.

To get the HTTP request object, inside of your action method, you can use the following code:

// Get HTTP request object
$request = $this->getRequest();

The code above returns the instance of Zend\Http\Request class, containing all the HTTP request data. In table 4.3, you can find the most widely used methods of the Request class together with their brief description.

Table 4.3. Methods of `Zend\Http\Request` class.
Method Name Description
isGet() Checks if this is a GET request.
isPost() Checks if this is a POST request.
isXmlHttpRequest() Checks if this request is an AJAX request.
isFlashRequest() Check if this request is a Flash request.
getMethod() Returns the method for this request.
getUriString() Returns the URI for this request object as a string.
getQuery($name, $default) Returns the query parameter by name, or all query parameters. If a parameter is not found, returns the $default value.
getPost($name, $default) Returns the parameter container responsible for post parameters or a single post parameter.
getCookie() Returns the Cookie header.
getFiles($name, $default) Returns the parameter container responsible for file parameters or a single file.
getHeaders($name, $default) Returns the header container responsible for headers or all headers of a certain name/type.
getHeader($name, $default) Returns a header by $name. If a header is not found, returns the $default value.
renderRequestLine() Returns the formatted request line (first line) for this HTTP request.
fromString($string) A static method that produces a Request object from a well-formed Http Request string
toString() Returns the raw HTTP request as a string.

Top