Whenever one of your application URLs are called, a request is received. This request carries the information that will be delivered to your application in order to get a proper response, this means, tell your application what to do and how to do, also sending data to your application.
In order to manage and work with this requests, Glowie has the
Glowie\Core\Http\Request class. This class has useful methods to help you handle every kind of request your application receives.
From a controller or middleware, you don't need to instantiate this class to work with it, simply use
$this->request property to directly access the
Request object. If you want to retrieve the instance from other places, use the static
Retrieving request details
In order to retrieve details about a request your application received, you can use the following functions:
$this->request->getURL()- Returns the full requested URL. This means, the entire URL (including query string parameters) the user typed to access your application.
$this->request->getURI()- Returns the clean requested URI. This means, only the URI (see Routes) the user typed. This does not includes the hostname or query string parameters.
$this->request->getIPAddress()- Returns the IP address used in the request, if available. If the IP address is not available, this method returns
$this->request->getMethod()- Returns the HTTP method used in the request.
trueif the request was made using AJAX. Note that information relies in the
trueif the request was made using a secure HTTPS connection.
Retrieving request data
In order to retrieve data that was passed through the request, use the
The first parameter is the variable key to get. The second is an optional default value. If the variable exists, its value will be returned. Otherwise, the default value will be returned.
This method can retrieve data passed from any kind of HTTP request method.
# user types in myappurl.com/search?query=products&page=1 $query = $this->request->getVar('products'); # returns "products" $page = $this->request->getVar('page') # returns "1"
To retrieve all the request data at once as an associative array, use
In order to get the raw request body as a string, use
If the request data was sent as a JSON string, you can also use
$this->request->getJson() to get an Element with the request data.
See Retrieving data for a list of shortcuts to retrieve request data.
Working with request headers
Headers are piece of information that each request carries with it. In order to retrieve a specific request header value, use the
The first argument is the name of the header you want to get, and the second is an optional default value. If the header exists, its value will be returned. Otherwise, the default value will be returned.
$token = $this->request->getHeader('token');
To retrieve all the request headers at once as an associative array, use
There are a few shortcuts to the most commonly used headers:
$this->request->getContentType()- Returns the
Content-Typeheader value or
nullif not available.
$this->request->getAuthorization()- Returns an Element with the username and password passed through a basic
Authorizationheader. If the header is not present or the Authorization is invalid, this method returns
$this->request->getPreviousUrl()- Returns the previous URL where the user was refered from. Note that this information relies in the
Refererheader. Be aware that this header cannot be always available.