Tutorials: Working with Requests

Getting Information

Framework collects via STDOUT MVC API all client request information, mostly detected from PHP superglobals, into a Lucinda\MVC\STDOUT\Request 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 Lucinda\MVC\STDOUT\UploadedFile tree available via getUploadedFiles() method.

This method works exactly like parameters(), preserving input name hierarchy, only that files themselves are encapsulated into UploadedFile 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->getUploadedFiles();

Variable $x will evaluate to:

[ "a"=>object(Lucinda\MVC\STDOUT\UploadedFile), "b"=>object(Lucinda\MVC\STDOUT\UploadedFile), "c"=>[ "x"=>object(Lucinda\MVC\STDOUT\UploadedFile), "y"=>object(Lucinda\MVC\STDOUT\UploadedFile) ] ]

Unless an error has occurred during upload, in which case a Lucinda\MVC\STDOUT\FileUploadException is thrown.

Getting Headers

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

Getting Route Validation Results

In order to perform routing, MVC API used by framework first needs to detect a <route> whose url matches that of requested page (see Routing section).

Final detection results are available via getValidator() method, which returns a Lucinda\MVC\STDOUT\RequestValidator object offering following methods:

Getting Path Parameters

Path parameters are detected during route validation process described above when a matching <route>'s url is patterned and made available to developers via following Lucinda\MVC\STDOUT\RequestValidator extra methods:

Setting Information

Everything collected by Lucinda\MVC\STDOUT\Request object is constant by default. In event listeners developers are able to add dynamic information via attributes using following methods:

Framework sets following Lucinda\MVC\STDOUT\Request attributes already:

Setting Method & Parameters Validation

Framework supports validation of requested resource (page) by url, value of request method and parameters. To make it possible, you need to open stdout.xml and make sure this tag exists within <listeners>:

<listener class="ValidationListener"/>

then edit <routes> tag, setting path to validator classes and policies for each <route> you desire to validate. Example:

<routes validators_path="application/validators"> <route url="user/save" controller="UserSaveController" view="user-save" method="POST"> <parameter name="name" validator="StringValidator"/> <parameter name="age" validator="AgeValidator" required="0"/> </route> ... </routes>

To learn more how to configure this tag, check official documentation!

Getting Method & Parameters Validation Results

After you have completed Setting Request Validation section described above, you are now able to check validation results using validation_results attribute defined by framework, which holds a Lucinda\RequestValidator\ResultsList instance.

Using Lucinda\RequestValidator\ResultsList you can query for global/individual validation results using its methods:

Let us imagine you want to validate a route pointing to /user/info/(id) GET request where id path parameter is a positive integer. To check if param value is a positive integer you need to create a Lucinda\RequestValidator\ParameterValidator:

class PositiveIntegerValidator extends Lucinda\RequestValidator\ParameterValidator { public function validate($value) { return (is_integer($value) && $value >= 0); } }

Then you need to put above class/file in application/models/validators folder and set stdout.xml accordingly:

<routes validators_path="application/models/validators"> <route url="user/info/(id)" method="GET"> <parameter name="id" validator="PositiveIntegerValidator"/> </route> ... </routes>

Once that is done, whenever user makes a request on that route, you can make sure validations pass (eg: in controllers) with:

if(!$this->request->attributes("validation_results")->hasPassed()) { throw new Lucinda\MVC\STDOUT\PathNotFoundException(); }

and get validator results for a particular parameter check with:

$x = $this->request->attributes("validation_results")->get("id"); // $x equals return value of validate method above

To learn more about how to create validators then work with their results, check official tutorials.


Share