logo

Tutorials: Working with Requests

Getting HTTP Request 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(Lucinda\STDOUT\Request\UploadedFiles\File), "b"=>object(Lucinda\STDOUT\Request\UploadedFiles\File), "c"=>[ "x"=>object(Lucinda\STDOUT\Request\UploadedFiles\File), "y"=>object(Lucinda\STDOUT\Request\UploadedFiles\File) ] ]

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()

Getting Console Request Information

Framework collects via Console MVC API all console request information, mostly detected from native php functions, into a object. All methods of this object are relevant for developers:

HTTP 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 @ .

How Is Request Method Validated

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

<route id="user/info/(id)" controller="Lucinda\Project\Controllers\UserInfo" 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 id="user/info/(id)" controller="Lucinda\Project\Controllers\UserInfo" view="user-info" method="GET"> <parameter name="id" validator="Lucinda\Project\ParameterValidators\PositiveInteger"/> </route>

Then you need to implement validator classes referenced in src/ParameterValidators folder, each extending . Example:

namespace Lucinda\Project\ParameterValidators; use Lucinda\STDOUT\EventListeners\Validators\ParameterValidator; class PositiveInteger implements ParameterValidator { 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 @ .

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(Lucinda\STDOUT\EventType::REQUEST, Lucinda\Project\EventListeners\HttpHeaders::class);

which requires a tag @ stdout.xml:

<headers cacheable="Lucinda\Project\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 these classes, but you can modify them if situation requires!

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

×