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: 2025-02-13

Request


in() - get Information about incoming Request

the in() method returns an object of DataType class DTRequestIn representing the incoming Request

method

public static function in() : DTRequestIn

usage

$oDTRequestIn = \MVC\Request::in();

For example

$sPath = \MVC\Request::in()->get_path();
$sQuery = \MVC\Request::in()->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::in()->get_requestMethod() === \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::in()->get_headerArray();

Example Result of $aHeader

// type: array, items: 22
[
    'X-Real-Ip' => '172.21.0.1',
    'X-Forwarded-Server' => '0d23c0701c03',
    'X-Forwarded-Proto' => 'https',
    'X-Forwarded-Port' => '443',
    'X-Forwarded-Host' => 'emvicy2x.ddev.site',
    'X-Forwarded-For' => '172.21.0.1',
    'Upgrade-Insecure-Requests' => '1',
    'Sec-Fetch-User' => '?1',
    'Sec-Fetch-Site' => 'none',
    'Sec-Fetch-Mode' => 'navigate',
    'Sec-Fetch-Dest' => 'document',
    'Sec-Ch-Ua-Platform' => '"Linux"',
    'Sec-Ch-Ua-Mobile' => '?0',
    'Sec-Ch-Ua' => '"Not(A:Brand";v="99", "Google Chrome";v="133", "Chromium";v="133"',
    'Priority' => 'u=0, i',
    'Cookie' => 'Emvicy_cookieConsent=true; Emvicy_secure=7t9i925cp5bendbl939ct4h6ug',
    'Cache-Control' => 'max-age=0',
    'Accept-Language' => 'de-DE,de;q=0.9,en-US;q=0.8,en;q=0.7',
    'Accept-Encoding' => 'gzip, deflate, br, zstd',
    'Accept' => '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.7',
    'User-Agent' => 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/133.0.0.0 Safari/537.36',
    'Host' => 'emvicy2x.ddev.site',
]

Get a certain header

Command

$aHeader = \MVC\Request::in()->getHeaderValueOnKey('User-Agent');

Example Result

string(10) "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/133.0.0.0 Safari/537.36"

Get data from body of current Request

Example PUT Request

curl -X PUT https://emvicy2x.ddev.site/api/1.0/foo/bar/ -H "Content-Type: application/json" -d '{"key": "value"}'

Command

$sInput = \MVC\Request::in()->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/*', '\Foo\Controller\Api::index');

Example Request

  • /api/1.0/foo/bar/what/else/

Get all Variables

Command

$aPathParam = \MVC\Request::in()->get_pathParamArray();

Example Result of $aPathParam

// type: array, items: 4
[
    'id' => '1',
    'name' => 'Foo',
    'address' => 'Bar',
    '_tail' => 'what/else/',
]

Get a certain Variable

Command

$aPathParam = \MVC\Request::in()->get_pathParamArray()['name'];

Example Result of $sPathParam

Foo

You can pass this function to the get function of Emvicy to receive an alternative value if the key you are looking for does not exist

Command

$aPathParam = get(\MVC\Request::in()->get_pathParamArray()['forename'], 'n/a');

Result

n/a

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/*', '\Foo\Controller\Index::foo');

Example Request

  • /foo/bar/baz/

Command

$sTail = \MVC\Request::in()->get_pathParamArray()['_tail'];

Result of $sTail

bar/baz/

Get Path Info

Get requested path as array

say the incoming Request is https://emvicy2x.ddev.site/imprint/foo/bar/baz

$aPath = Request::in()->get_pathArray();

Result of $aPath

// type: array, items: 4
[
    0 => 'imprint',
    1 => 'foo',
    2 => 'bar',
    3 => 'baz',
]

Enquiry with any url

$aPath = RequestHelper::getPathArrayOnUrl('https://www.example.com/Imprint/')

Result of $aPath

// type: array, items: 1
[
    0 => 'Imprint',
]

Sanitizing

sanitizing input (e.g. PUT)

$oDTRequestIn = \MVC\Request::in();

// sanitizing
$oDTRequestIn->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($oDTRequestIn->get_input(), 0, 256)
    )
);

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

out() - perform an outgoing Request

perform a local GET request on /api/

/** @var \MVC\DataType\DTResponse $oDTResponse */
$oDTResponse = Request::out(
    DTRequestOut::create()
        ->set_eRequestMethod(EnumRequestMethod::GET)
        ->set_sUrl('/api/')
);

Example Response \MVC\DataType\DTResponse $oDTResponse

\MVC\DataType\DTResponse::__set_state(array(
      'body' => '{"requestMethod":"GET", ...',
      'raw' => 'HTTP/1.1 200 OK
          Content-Security-Policy: default-src \'self\'; ...
          Content-Type: application/json
          Date: Thu, 06 Feb 2025 13:17:44 GMT
          Server: Apache/2.4.62 (Debian)
          Strict-Transport-Security: max-age=63072000
          X-Content-Security-Policy: default-src \'self\'; ...
          X-Frame-Options: allow-from \'none\'
          X-Webkit-Csp: default-src \'self\'; ...
          X-Xss-Protection: 1; mode=block
          Connection: close
          Transfer-Encoding: chunked

          {"requestMethod":"GET", ...',
      'headers' =>    array (
        'content-security-policy' => 'default-src \'self\'; ...',
        'content-type' => 'application/json',
        'date' => 'Thu, 06 Feb 2025 13:17:44 GMT',
        'server' => 'Apache/2.4.62 (Debian)',
        'strict-transport-security' => 'max-age=63072000',
        'x-content-security-policy' => 'default-src \'self\'; ...',
        'x-frame-options' => 'allow-from \'none\'',
        'x-webkit-csp' => 'default-src \'self\'; ...',
        'x-xss-protection' => '1; mode=block',
        'connection' => 'close',
        'transfer-encoding' => 'chunked',
    ),
      'status_code' => 200,
      'protocol_version' => 1.1,
      'success' => true,
      'redirects' => 0,
      'url' => 'https://emvicy2x.ddev.site/api/',
      'history' =>    array (
    ),
      'cookies' =>    array (
        'cookies' =>        array (
        ),
    ),
))

perform a remote GET request

/** @var \MVC\DataType\DTResponse $oDTResponse */
$oDTResponse = Request::out(
    DTRequestOut::create()
        ->set_eRequestMethod(EnumRequestMethod::GET)
        ->set_sUrl('https://api.ddev.site/table/address/2/')
        ->set_aHeader(array(
            'accept' => Type_Application_json::DESCRIPTION,
            'apikey' => getenv('apikey')
        ))
);