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: 2023-07-25

Request


Get current Request

Example GET Request

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

Command

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

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.

Check

$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 header of current Request

Get all headers

Command

$aHeader = \MVC\Request::getHeaderArray();

Example Result

array(10) {
  ["Host"]=>string(23) "mymvcdoku.ueffing.local"
  ["Connection"]=>string(10) "keep-alive"
  ["Cache-Control"]=>string(9) "max-age=0"
  ["Upgrade-Insecure-Requests"]=>string(1) "1"
  ["User-Agent"]=>string(101) "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/106.0.0.0 Safari/537.36"
  ["Accept"]=>string(135) "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9"
  ["Referer"]=>string(44) "http://mymvcdoku.ueffing.local/3.3.x/request"
  ["Accept-Encoding"]=>string(13) "gzip, deflate"
  ["Accept-Language"]=>string(35) "de-DE,de;q=0.9,en-US;q=0.8,en;q=0.7"
  ["Cookie"]=>string(58) "myMVC_cookieConsent=true; myMVC=0j6eatdmvbq8tqsnsoeph6kipd"
}

Get a certain header

Command

$aHeader = \MVC\Request::getHeader('Connection');

Example Result

string(10) "keep-alive"

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();