Tutorials: Working with Requests

Getting Information

Framework collects via STDOUT MVC API all client request information, mostly detected from PHP superglobals, into a object. All methods of this object are relevant for developers!

Getting Basic Information

Following methods can be used to retrieve basic information

Getting Request Parameters

Request parameters are detected by matching HTTP request method to value of respective superglobal (eg: if method was POST, then $_POST is assumed) or input stream (for PUT, DELETE) and become available via following methods:

Getting Uploaded Files

Uploaded files are detected based on contents of $_FILES superglobal. Latter structure is too clumsy and unintuitive, so it had to be remodelled into an tree available via uploadedFiles() method.

This method works exactly like parameters(), preserving input name hierarchy, only that files themselves are encapsulated into objects. When, for example, following files are uploaded:

<input type="file" name="a"> <input type="file" name="b"> <input type="file" name="c[x]"> <input type="file" name="c[y]">

Then when Controller runs this statement:

$x = $this->request->uploadedFiles();

Variable $x will evaluate to:

[ "a"=>object(), "b"=>object(), "c"=>[ "x"=>object(), "y"=>object() ] ]

Unless an error has occurred during upload, in which case a is thrown.

Getting Headers

Getting request headers works in exactly the same way as parameters via following Request methods:

If you desire to receive HTTP request headers in encapsulated form according to ISO standards, follow steps described in How To Validate Request Headers then use $this->attributes->getHeaders()->getRequest() to access them as instance. Example:

$this->attributes->getHeaders()->getRequest()->getAcceptLanguage()

Request Validation

Once a request is received, framework needs to match it with a tag (see Routing section). Validation results are collected into object, which makes them available via following methods:

How is response format validated?

Format to resolve views into is detected according to following algorithm:

  1. if format attribute @ exists
  2. otherwise, value of default_format attribute @ is returned

How Is Request URI Validated

Final request uri is detected according to following algorithm:

Results of request uri validation will be collected and made available via getValidPage and getPathParameters methods above.

How Is Request Method Validated

Request method a route allows is validated based on method attribute @ . Example:

<route url="user/info/(id)" controller="UserInfoController" view="user-info" method="GET"/>

In case above, if route is called using a different request method from GET, a is thrown!

How Are Request Parameters Validated

Framework supports validation of both path and request parameters (if names collide, former takes precedence). For each parameter you want to validate, you must set a <parameter> tag child to holding the policy. Example:

<route url="user/info/(id)" controller="UserInfoController" view="user-info" method="GET"> <parameter name="id" validator="PositiveIntegerValidator"/> </route>

Then you need to implement validator classes referenced, each extending . Example:

class PositiveIntegerValidator implements { public function validate($value) { return (is_integer($value) && $value >= 0); } }

If validate returns NULL, a is thrown! Otherwise, its result is collected by parameter name and will be made available via getValidParameters method above.

How To Validate Request Headers

To perform automatic HTTP request headers validation you need to open bootstrap index.php file and add this event listener:

$object->addEventListener(::REQUEST, "HttpHeadersListener");

which requires a tag @ stdout.xml:

<headers cacheable="application/cacheables/[DRIVER]"/>

Where [DRIVER] is a implementation, able to get a string / date representation of response about to be sent. Framework comes by default with:

Following types of request header validation are supported, each requiring an extra event listener:

×