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".  
Status: 2022-10-20


Get current Request

Example GET Request



$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.


$bMethodMatch = (
    // any request method is allowed
    '*' === \MVC\Route::getCurrent()->get_method() ||
    // request method does match route method
    \MVC\Request::getServerRequestMethod() === \MVC\Route::getCurrent()->get_method()
) ? true : false;

Evaluate and React

if (false === $bMethodMatch)
    die('wrong request method `' 
        . \MVC\Request::getServerRequestMethod() 
        . '`. It has to be one of: `' 
        . implode('|', Route::getCurrent()->get_methodsAssigned()) . '`'

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"}'


$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


$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


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

Example Result of $sPathParam


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/


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

Result of $sTail


If you want the Result as an array

$aTail = \MVC\Request::getPathArray(

Result of $aTail

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


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 */
        '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
        // sanitizing by regex rule
        // sanitizing by string length
        substr($oDTRequestCurrent->get_input(), 0, 256)

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