HTTP Request
In addition to the native PHP features, there are also additional specific features to make it easier to handle HTTP requests.
Determine types of requests
Checks if the request matches a specific type: HTTPS, XHR, Pjax, prefetch, save-data, GPC, or a standard HTTP method (e.g., GET, POST).
| Usage | Description |
|---|---|
Request::is('gpc') |
Returns true if sent Sec-GPC: 1 header in request, otherwise returns false.
|
Request::is('pjax') |
Returns true if sent X-Pjax header in request, otherwise returns false.
|
Request::is('prefetch') |
Returns true if sent prefetch header (e.g., Sec-Purpose, x-purpose, purpose, x-moz) in request, otherwise returns false.
|
Request::is('save') |
Returns true if sent Save-Data: on header in request, otherwise returns false.
|
Request::is('secure') |
Returns true if using HTTPS, otherwise returns false.
|
Request::is('xhr') |
Returns true if sent X-Requested-With: XMLHttpRequest header in request, otherwise returns false.
|
Obtaining headers
To get the current header, use:
use Inphinit\Http\Request;
echo Request::header('content-length'), '<br>';
echo Request::header('content-type'), '<br>';
echo Request::header('accept');
If a header is not defined by default, Request::header() will return null; however, it is still possible to change the alternative value, as in the following example:
use Inphinit\Http\Request;
echo Request::header('x-foo', 'Missing Value!');
If X-Foo is not in the request, Missing Value! will be displayed.
Obtaining querystring
This method returns exactly the same value as $_SERVER['QUERY_STRING'], with one difference: if you are on an IIS server, some requests to pages called by <error statusCode="code" responseMode="ExecuteURL" ... may return an incorrect value. This method will only return a querystring when absolutely necessary.
use Inphinit\Http\Request;
echo Request::query();
Obtaining GET, POST, and COOKIE
In PHP, requests accept nested values, so writing code like this:
echo $_GET['foo']['bar']['baz'];
echo $_POST['foo']['bar']['baz'];
echo $_COOKIE['foo']['bar']['baz'];
To simplify things, you can use:
use Inphinit\Http\Request;
echo Request::get('foo.bar.baz');
echo Request::post('foo.bar.baz');
echo Request::cookie('foo.bar.baz');
If a value does not exist, you can return an alternative value, for example:
use Inphinit\Http\Request;
echo Request::get('foo.bar.baz', 'Missing get!');
echo Request::post('foo.bar.baz', 'Missing post!');
echo Request::cookie('foo.bar.baz', 'Missing cookie!');
Obtaining the request path
To get the current URL path, without the querystring, you can use the method
use Inphinit\Http\Request;
echo Request::path();
The return value of Request::path() is similar to INPHINIT_PATH, with the difference that INPHINIT_PATH only returns the path relative to the application. For better understanding, due to the flexibility of Inphinit, it's possible to use it without needing to create a VirtualHost. So, if Apache has a server with folders, just like a simply configured server:
DocumentRoot "/var/www/html"
<Directory "/var/www/html">
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
So within the /var/www/html folder it's possible to serve different applications via folders, for example:
-
inphinit-app/
- .htaccess
- index.php
-
public/
- .htaccess
-
system/
- main.php
-
other-application/
- index.php
- sample.php
-
blog/
- .htaccess
- index.php
- wp-admin/
So if you have a route like:
use Inphinit\Http\Request;
$app->action('GET', '/foo/bar/baz', function () {
var_dump(Request::path());
var_dump(INPHINIT_PATH);
});
So when you access http://example/inphinit-app/foo/bar/baz you will get:
string(25) "/inphinit-app/foo/bar/baz"
string(12) "/foo/bar/baz"