Request
- Get current Request
- Get data from body of current Request
- Accessing Path Params / Variables
- Sanitizing
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.
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 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');
- for more Information about setting up such routes, see Routing with Path Params / Variables
Example Request
/api/1/Foo/Bar/what/else/
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/"
}
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');
- see Wildcard routing
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();