The Request object describes the current web execution context for PHP. Note
that it is not an HTTP request object proper, since it includes things
like $_ENV
and various non-HTTP $_SERVER
keys.
You can get the Request object from the DI,
The Request object contains several property objects. Some represent a copy of the PHP superglobals …
$request->cookies
for $_COOKIES
$request->env
for $_ENV
$request->files
for $_FILES
$request->post
for $_POST
$request->query
for $_GET
$request->server
for $_SERVER
… and others represent more specific kinds of information about the request:
$request->client
for the client making the request$request->content
for the raw body of the request$request->headers
for the request headers$request->method
for the request method$request->accept
for content negotiation$request->params
for path-info parameters$request->url
for the request URLThe Request object has only one method, isXhr()
, to indicate if the
request is an XmlHttpRequest or not.
Each of the superglobal representation objects has a single method, get()
,
that returns the value of a key in the superglobal, or an alternative value
if the key is not present. The values here are read-only.
The $request->client
object has these methods:
getForwardedFor()
returns the values of the X-Forwarded-For
headers as
an array.
getReferer()
returns the value of the Referer
header.
getIp()
returns the value of $_SEVER['REMOTE_ADDR']
, or the appropriate
value of X-Forwarded-For
.
getUserAgent()
return the value of the User-Agent
header.
isCrawler()
returns true if the User-Agent
header matches one of a list
of bot/crawler/robot user agents (otherwise false).
isMobile()
returns true if the User-Agent
header matches one of a list
of mobile user agents (otherwise false).
The $request->content
object has these methods:
getType()
returns the content-type of the request body
getRaw()
return the raw request body
get()
returns the request body after decoding it based on the content type
The Content object has two decoders built in.
If the request specified a content type of application/json
,
the get()
method will automatically decode the body with json_decode()
.
Likewise, if the content type is application/x-www-form-urlencoded
, the
get()
method will automatically decode the body with parse_str()
.
The $request->headers
object has a single method, get()
, that returns the
value of a particular header, or an alternative value if the key is not
present. The values here are read-only.
The $request->method
object has these methods:
get()
: returns the request method valueisDelete()
: Did the request use a DELETE method?isGet()
: Did the request use a GET method?isHead()
: Did the request use a HEAD method?isOptions()
: Did the request use an OPTIONS method?isPatch()
: Did the request use a PATCH method?isPut()
: Did the request use a PUT method?isPost()
: Did the request use a POST method?You can also call is*()
on the Method object; the part after is
is
treated as custom HTTP method name, and checks if the request was made using
that HTTP method.
Sometimes forms use a special field to indicate a custom HTTP method on a
POST. By default, the Method object honors the _method
form field.
Unlike most Request property objects, the Params object is read-write (not read-only). The Params object allows you to set application-specific parameter values. These are typically discovered by parsing a URL path through a router of some sort (e.g. Aura.Router).
The $request->params
object has two methods:
set()
to set the array of parametersget()
to get back a specific parameter, or the array of all parametersFor example:
The $request->url
object has two methods:
get()
returns the full URL string; or, if a component constant is passed,
returns only that part of the URL
isSecure()
indicates if the request is secure, whether via SSL, TLS, or
forwarded from a secure protocol