Skip to content

Request

Mistralys edited this page Oct 25, 2019 · 4 revisions

The Request class

This class simplifies access to request parameters, along with a number of validation methods.

Configuration

No configuration needed: simply create a new instance of the Request class.

$request = new \AppUtils\Request();

Getting parameters

The basic way is to access parameters without any validation:

$param = $request->getParam('foo');

By default, if a parameter does not exist, this will return NULL. You can specify a default value instead:

// if the parameter does not exist, return `bar`
$param = $request->getParam('foo', 'bar');

NOTE: Since the getParam method is type independent, it is recommended to always specify a default value.

Validating parameters

To validate parameters, you have to register them. Once registered, the validation will be applied automatically every time you with to access it.

For example, if you have a parameter that contains an integer:

// register the parameter once
$request->registerParam('myparam')->setInteger();

// When retrieving it, the validation is applied.
$int = $request->getParam('myparam', 0);

If you only need to access a parameter once, you can use this shorter syntax:

$int = $request->registerParam('myparam')->setInteger()->get(0);

Filtering parameters

Filters can be run on the parameters before they are validated. This is useful for sanitizing values before accessing them, but also to allow more complex value scenarios.

For example, adding a simple trim filter to remove white space around the parameter:

$param = $request->registerParam('foo')->addFilterTrim()->get('');

Filters can be chained: they are applied in the order they are added to the parameter.

// this will trim spaces left over after stripping tags.
$param = $request->registerParam('foo')
->addStripTagsFilter()
->addFilterTrim()
->get();

Validation types

Integer

The integer validation ensures that only valid integer values are accepted. Any other values are simply ignored.

Note: By default, the get method will return NULL. Specify a default value to avoid type conflicts.

$id = $request->registerParam('myid')->setInteger()->get(0);

Examples

Comma-separated list of known values

This is a handy way of passing multiple values, comma-separated in a single parameter. Only the allowed values will be kept during the validation.

// Assuming the parameter has this value:
$_REQUEST['myvalues'] = 'bar,othervalue,foo';

$values = $this->request->registerParam('myvalues')
->addCommaSeparatedFilter() // split the values by commas
->setValuesList(array('foo', 'bar')) // only these values are allowed
->get();

This will return an array with only the allowed parameters:

array(
    'bar',
    'foo'
)

New here?

Have a look at the overview for a list of all helper classes available in the package.

Table of contents

Find the current page in the collapsible "Pages" list above, and expand the page, to view a table of contents.

Clone this wiki locally