The request object

As we said, the request object will encapsulate all the data related to a request and provide the developer with a fluent API to access the data. Let's take a look at the details of the request object in order to understand how to use it and what it can offer to us:

use ZendHttpRequest;

$string = "GET /foo HTTP/1.1

Some Content";
$request = Request::fromString($string);

$request->getMethod();
$request->getUri();
$request->getUriString();
$request->getVersion();
$request->getContent();

This example comes directly from the documentation and shows how a request object can be created from a string, and then access some data related with the request using the methods provided. So, every time we need to know something related to the request, we will access this object to get the data we need.

If we check the code on ZendHttpPhpEnvironmentRequest.php, the first thing we can notice is that the data is populated on the constructor using the superglobal arrays. All this data is processed and then populated inside the object to be able to expose it in a standard way using methods.

To manipulate the URI of the request you can get/set the data with three methods, two getters and one setter. The only difference with the getters is that one returns a plain string and the other returns an HttpUri object.

  • getUri() and getUriString()
  • setUri()

To retrieve the data passed in the request, there are a few specialized methods depending on the data you want to get:

  • getQuery()
  • getPost()
  • getFiles()
  • getHeader() and getHeaders()

About the request method, the object has a general way to know the method used, returning a string or nine specialized functions that will test specific methods based on the RFC 2616, which defines the standard methods for an HTTP request.

  • getMethod()
  • isOptions()
  • isGet()
  • isHead()
  • isPost()
  • isPut()
  • isDelete()
  • isTrace()
  • isConnect()
  • isPatch()

Finally, two more methods are available in this object that will test special requests such as AJAX and requests made by a flash object.

  • isXmlHttpRequest()
  • isFlashRequest()

Notice that the data stored on the superglobal arrays when populated on the object are converted from an Array to a Parameters object.

The Parameters object lives in the Stdlib section of ZF2, a folder where common objects can be found and used across the framework. In this case, the Parameters class is an extension of ArrayObject and implements ParametersInterface that will bring ArrayAccess, Countable, Serializable, and Traversable functionality to the parameters stored inside the object. The goal with this object is to provide a common interface to access data stored in the superglobal arrays. This expands the ways you can interact with the data in an object-oriented approach.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.29.219