DEV Community

loading...
Cover image for Mezon wrapper for processing HTTP requests

Mezon wrapper for processing HTTP requests

alexdodonov
PHP developer
Updated on ・2 min read

This class will help you to fetch data from $_POST, $_GET arrays and headers.

Installation

Just print in console

composer require mezon/request
Enter fullscreen mode Exit fullscreen mode

And that's all )

Learn more

More information can be found here:

Twitter

dev.to

Slack

How to start

The firs steps are quite simple

var_dump(Request::getParam('some-param', 'default'));// will be outputted 'default'

$_GET['some-param'] = 'some-value';
var_dump(Request::getParam('some-param'));// will be outputted 'some-value'
Enter fullscreen mode Exit fullscreen mode

Fields priorities

In case you have the same parameters in $_GET, $_POST and other global arrays, then they will be prioritized in this way:

  • security tokens in HTTP headers
  • Router parameters
  • HTTP request headers
  • $_POST
  • $_GET

For example:

$_GET['some-param'] = 'get-value';
$_POST['some-param'] = 'post-value';

var_dump(Request::getParam('some-param'));// will be outputted 'post value'
Enter fullscreen mode Exit fullscreen mode

Router parameters

You can pass your Router object to this class and fetch parameters for non-static routes:

Request::registerRouter(<your Mezon\Router object>);
Enter fullscreen mode Exit fullscreen mode

Security tokens in HTTP headers

There are a way to fetch security token from headers:

  • Authentication
  • Authorization
  • Cgi-Authorization

For example if you will pass in headers something like that:

Authorization: Basic <some token>
Enter fullscreen mode Exit fullscreen mode

And then call:

Request::getParam('session_id')
Enter fullscreen mode Exit fullscreen mode

Then this call will return .

Wrappers

There are some convenient wrappers were implemented:

// will return true if the parameter exists
// will return false otherwise
Request::wasSubmitted('param-name')
Enter fullscreen mode Exit fullscreen mode

Or wrapper for check-boxes:

Request::getChecked('param-name', ['switched on', 'switched off'])
Request::getChecked('param-name', [1, 0])
Request::getChecked('param-name', [true, false])
Enter fullscreen mode Exit fullscreen mode

In this call the method getChecked will return the first element of the array which is passed as the second parameter. And the second element otherwise.

Learn more

More information can be found here:

Twitter
Mezon Framework

Discussion (0)