Flight encapsulates the HTTP request into a single object, which can be accessed by doing:
$request = Flight::request();
When you are working with a request in a web application, typically you'll want to pull out a header, or a $_GET or $_POST parameter, or maybe even the raw request body. Flight provides a simple interface to do all of these things.
$_GET
$_POST
Here's an example getting a query string parameter:
Flight::route('/search', function(){ $keyword = Flight::request()->query['keyword']; echo "You are searching for: $keyword"; // query a database or something else with the $keyword });
Here's an example of maybe a form with a POST method:
Flight::route('POST /submit', function(){ $name = Flight::request()->data['name']; $email = Flight::request()->data['email']; echo "You submitted: $name, $email"; // save to a database or something else with the $name and $email });
The request object provides the following properties:
$_SERVER
HTTP_CLIENT_IP
HTTP_X_FORWARDED_FOR
HTTP_X_FORWARDED
HTTP_X_CLUSTER_CLIENT_IP
HTTP_FORWARDED_FOR
HTTP_FORWARDED
You can access the query, data, cookies, and files properties as arrays or objects.
query
data
cookies
files
So, to get a query string parameter, you can do:
$id = Flight::request()->query['id'];
Or you can do:
$id = Flight::request()->query->id;
To get the raw HTTP request body, for example when dealing with PUT requests, you can do:
$body = Flight::request()->getBody();
If you send a request with the type application/json and the data {"id": 123} it will be available from the data property:
application/json
{"id": 123}
$id = Flight::request()->data->id;
You can access the $_GET array via the query property:
You can access the $_POST array via the data property:
$id = Flight::request()->data['id'];
You can access the $_COOKIE array via the cookies property:
$_COOKIE
$myCookieValue = Flight::request()->cookies['myCookieName'];
There is a shortcut available to access the $_SERVER array via the getVar() method:
getVar()
$host = Flight::request()->getVar['HTTP_HOST'];
You can access uploaded files via the files property:
$uploadedFile = Flight::request()->files['myFile'];
You can access request headers using the getHeader() or getHeaders() method:
getHeader()
getHeaders()
// Maybe you need Authorization header $host = Flight::request()->getHeader('Authorization'); // or $host = Flight::request()->header('Authorization'); // If you need to grab all headers $headers = Flight::request()->getHeaders(); // or $headers = Flight::request()->headers();
You can access the raw request body using the getBody() method:
getBody()
You can access the request method using the method property or the getMethod() method:
method
getMethod()
$method = Flight::request()->method; // actually calls getMethod() $method = Flight::request()->getMethod();
Note: The getMethod() method first pulls the method from $_SERVER['REQUEST_METHOD'], then it can be overwritten by $_SERVER['HTTP_X_HTTP_METHOD_OVERRIDE'] if it exists or $_REQUEST['_method'] if it exists.
$_SERVER['REQUEST_METHOD']
$_SERVER['HTTP_X_HTTP_METHOD_OVERRIDE']
$_REQUEST['_method']
There are a couple helper methods to piece together parts of a URL for your convenience.
You can access the full request URL using the getFullUrl() method:
getFullUrl()
$url = Flight::request()->getFullUrl(); // https://example.com/some/path?foo=bar
You can access the base URL using the getBaseUrl() method:
getBaseUrl()
$url = Flight::request()->getBaseUrl(); // Notice, no trailing slash. // https://example.com
You can pass a URL to the parseQuery() method to parse the query string into an associative array:
parseQuery()
$query = Flight::request()->parseQuery('https://example.com/some/path?foo=bar'); // ['foo' => 'bar']