This website uses Cookies to provide you with the best possible service. Please see our Privacy Policy for more information. Click the check box below to accept cookies. Then confirm with a click on "Save".  

Request


Get current Request

Example GET Request

http://mymvc.ueffing.local/foo/bar/?a=1;b=2;c=3

Command

$oDTRequestCurrent = \MVC\Request::getCurrentRequest()

Example Result of $oDTRequestCurrent

object(MVC\DataType\DTRequestCurrent)#65 (9) {
  ["scheme":protected]=>string(4) "http"
  ["host":protected]=>string(19) "mymvc.ueffing.local"
  ["path":protected]=>string(9) "/foo/bar/"
  ["query":protected]=>string(11) "a=1;b=2;c=3"
  ["requesturi":protected]=>string(21) "/foo/bar/?a=1;b=2;c=3"
  ["requestmethod":protected]=>string(3) "GET"
  ["protocol":protected]=>string(7) "http://"
  ["full":protected]=>string(47) "http://mymvc.ueffing.local/foo/bar/?a=1;b=2;c=3"
  ["input":protected]=>string(0) ""
}

As it gives you an object of type MVC\DataType\DTRequestCurrent, you can access all key/values by a getter.

For example

$sPath = \MVC\Request::getCurrentRequest()->get_path();
$sQuery = \MVC\Request::getCurrentRequest()->get_query();

check request method against route method

Check if request method equals the expecting one you declared in your route.

\MVC\Request::getServerRequestMethod() < = ? = > \MVC\Route::getCurrent()->get_method()

Example

if (\MVC\Request::getServerRequestMethod() !== \MVC\Route::getCurrent()->get_method())
{
    die('wrong request method `' 
        . \MVC\Request::getServerRequestMethod() 
        . '`. It has to be: `' 
        . \MVC\Route::getCurrent()->get_method() . '`'
    );
}

Get data from body of current Request

Example PUT Request

curl -X PUT http://mymvc.ueffing.local/api/1.0.0/user/1969/ -H "Content-Type: application/json" -d '{"key": "value"}'

Command

$sInput = \MVC\Request::getCurrentRequest()->get_input();

Example Result

{"key": "value"}
  • As you can see here, input contains the values we PUT ({"key": "value"})

Accessing Path Params / Variables

Example route

\MVC\Route::get('/api/:id/:name/:address/*', 'module=Foo&c=Api&m=index');

Example Request

  • /api/1/Foo/Bar/what/else/

Get all Variables

Command

$aPathParam = \MVC\Request::getPathParam();

Example Result of $aPathParam

array(4) {
  ["id"]=>string(1) "1"
  ["name"]=>string(3) "Foo"
  ["address"]=>string(3) "Bar"
  ["_tail"]=>string(10) "what/else/"
}

Get a certain Variable

Command

$sPathParam = \MVC\Request::getPathParam('name')

Example Result of $sPathParam

Foo

Get the overlapping string on wildcard route paths

say you have a wildcard route and you want to get the overlapping path string after *.

Example route

\MVC\Route::get('/foo/*', 'module=Foo&c=Index&m=foo');

Example Request

  • /foo/bar/baz/

Command

$sTail = \MVC\Request::getPathParam()['_tail'];

Result of $sTail

bar/baz/

If you want the Result as an array

$aTail = \MVC\Request::getPathArray(
    \MVC\Request::getPathParam()['_tail']
);

Result of $aTail

array(2) {
  [0]=>string(3) "bar"
  [1]=>string(1) "baz"
}

Sanitizing

You can define rules for sanitizing any $_GET, $_POST, $_COOKIE parameter for a request.

sanitizing $_GET, $_POST, $_COOKIE

\MVC\Request::sanitize('GET', array(
    // rules for parameter `a`
    'a' => array(
        /** @see https://www.regular-expressions.info/unicode.html */
        'regex' => "/[^\\p{L}\\p{M}\\p{Z}\\p{S}\\p{N}\\p{P}\|']+/u",#
        'length' => 256,
    ),
));

sanitizing input (e.g. PUT)

$oDTRequestCurrent = \MVC\Request::getCurrentRequest();

// sanitizing
$oDTRequestCurrent->set_input(
    preg_replace(
        // sanitizing by regex rule
        "/[^\\p{L}\\p{M}\\p{Z}\\p{S}\\p{N}\\p{P}\|']+/u",
        '',
        // sanitizing by string length
        substr($oDTRequestCurrent->get_input(), 0, 256)
    )
);

// sanitized
$sInput = $oDTRequestCurrent->get_input();