ServerRequest
in package
implements
ServerRequestInterface
A class that helps wrap Request information and particulars about a single request.
Provides methods commonly used to introspect on the request headers and request body.
Table of Contents
Interfaces
- ServerRequestInterface
- Representation of an incoming, server-side HTTP request.
Properties
- $trustProxy : bool
- Whether to trust HTTP_X headers set by most load balancers.
- $_detectorCache : array<string, bool>
- Instance cache for results of is(something) calls
- $_detectors : array<string|int, callable|array<string|int, mixed>>
- The built in detectors used with `is()` can be modified with `addDetector()`.
- $_environment : array<string, mixed>
- Array of environment data.
- $attributes : array<string, mixed>
- Store the additional attributes attached to the request.
- $base : string
- Base URL path.
- $cookies : array<string, mixed>
- Array of cookie data.
- $data : object|array<string|int, mixed>|null
- Array of POST data. Will contain form data as well as uploaded files.
- $emulatedAttributes : array<string|int, string>
- A list of properties that emulated by the PSR7 attribute methods.
- $flash : FlashMessage
- Instance of a FlashMessage object relative to this request
- $params : array<string|int, mixed>
- Array of parameters parsed from the URL.
- $protocol : string|null
- The HTTP protocol version used.
- $query : array<string|int, mixed>
- Array of query string arguments
- $requestTarget : string|null
- The request target if overridden
- $session : Session
- Instance of a Session object relative to this request
- $stream : StreamInterface
- Request body stream. Contains php://input unless `input` constructor option is used.
- $trustedProxies : array<string|int, string>
- Trusted proxies list
- $uploadedFiles : array<string|int, mixed>
- Array of Psr\Http\Message\UploadedFileInterface objects.
- $uri : UriInterface
- Uri instance
- $webroot : string
- webroot path segment for the request.
Methods
- __call() : bool
- Missing method handler, handles wrapping older style isAjax() type methods
- __construct() : mixed
- Create a new request object.
- acceptLanguage() : array<string|int, mixed>|bool
- Get the languages accepted by the client, or check if a specific language is accepted.
- accepts() : array<string|int, string>|bool
- Find out which content types the client accepts or check if they accept a particular type of content.
- addDetector() : void
- Add a new detector to the list of detectors that a request can use.
- allowMethod() : true
- Allow only certain HTTP request methods, if the request method does not match a 405 error will be shown and the required "Allow" response header will be set.
- clearDetectorCache() : void
- Clears the instance detector cache, used by the is() function
- clientIp() : string
- Get the IP the client is using, or says they are using.
- contentType() : string|null
- Get the content type used in this request.
- domain() : string
- Get the domain name and include $tldLength segments of the tld.
- getAttribute() : mixed
- Read an attribute from the request, or get the default
- getAttributes() : array<string, mixed>
- Get all the attributes in the request.
- getBody() : StreamInterface
- Gets the body of the message.
- getCookie() : array<string|int, mixed>|string|null
- Read cookie data from the request's cookie data.
- getCookieCollection() : CookieCollection
- Get a cookie collection based on the request's cookies
- getCookieParams() : array<string, mixed>
- Get all the cookie data from the request.
- getData() : mixed
- Provides a safe accessor for request data. Allows you to use Hash::get() compatible paths.
- getEnv() : string|null
- Get a value from the request's environment data.
- getFlash() : FlashMessage
- Returns the instance of the FlashMessage object for this request
- getHeader() : array<string|int, string>
- Get a single header from the request.
- getHeaderLine() : string
- Get a single header as a string from the request.
- getHeaders() : array<string|int, array<string|int, string>>
- Get all headers in the request.
- getMethod() : string
- Get the HTTP method used for this request.
- getParam() : mixed
- Safely access the values in $this->params.
- getParsedBody() : object|array<string|int, mixed>|null
- Get the parsed request body data.
- getPath() : string
- Get the path of current request.
- getProtocolVersion() : string
- Retrieves the HTTP protocol version as a string.
- getQuery() : array<string|int, mixed>|string|null
- Read a specific query value or dotted path.
- getQueryParams() : array<string|int, mixed>
- Get all the query parameters in accordance to the PSR-7 specifications. To read specific query values use the alternative getQuery() method.
- getRequestTarget() : string
- Retrieves the request's target.
- getServerParams() : array<string|int, mixed>
- Get all the server environment parameters.
- getSession() : Session
- Returns the instance of the Session object for this request
- getTrustedProxies() : array<string|int, string>
- Get trusted proxies
- getUploadedFile() : UploadedFileInterface|null
- Get the uploaded file from a dotted path.
- getUploadedFiles() : array<string|int, mixed>
- Get the array of uploaded files from the request.
- getUri() : UriInterface
- Retrieves the URI instance.
- hasHeader() : bool
- Check if a header is set in the request.
- host() : string|null
- Get the host that the request was handled on.
- input() : mixed
- Read data from `php://input`. Useful when interacting with XML or JSON request body content.
- is() : bool
- Check whether a Request is a certain type.
- isAll() : bool
- Check that a request matches all the given types.
- parseAccept() : array<string|int, mixed>
- Parse the HTTP_ACCEPT header and return a sorted array with content types as the keys, and pref values as the values.
- port() : string|null
- Get the port the request was handled on.
- referer() : string|null
- Returns the referer that referred this request.
- scheme() : string|null
- Get the current url scheme used for the request.
- setTrustedProxies() : void
- register trusted proxies
- subdomains() : array<string|int, string>
- Get the subdomains for a host.
- withAddedHeader() : static
- Get a modified request with the provided header.
- withAttribute() : static
- Return an instance with the specified request attribute.
- withBody() : static
- Return an instance with the specified message body.
- withCookieCollection() : static
- Replace the cookies in the request with those contained in the provided CookieCollection.
- withCookieParams() : static
- Replace the cookies and get a new request instance.
- withData() : static
- Update the request with a new request data element.
- withEnv() : static
- Update the request with a new environment data element.
- withHeader() : static
- Get a modified request with the provided header.
- withMethod() : static
- Update the request method and get a new instance.
- withoutAttribute() : static
- Return an instance without the specified request attribute.
- withoutData() : static
- Update the request removing a data element.
- withoutHeader() : static
- Get a modified request without a provided header.
- withParam() : static
- Update the request with a new routing parameter
- withParsedBody() : static
- Update the parsed body and get a new instance.
- withProtocolVersion() : static
- Return an instance with the specified HTTP protocol version.
- withQueryParams() : static
- Update the query string data and get a new instance.
- withRequestTarget() : static
- Create a new instance with a specific request-target.
- withUploadedFiles() : static
- Update the request replacing the files, and creating a new instance.
- withUri() : static
- Return an instance with the specified uri
- _acceptHeaderDetector() : bool
- Detects if a specific accept header is present.
- _environmentDetector() : bool
- Detects if a specific environment variable is present.
- _headerDetector() : bool
- Detects if a specific header is present.
- _is() : bool
- Worker for the public is() function
- _paramDetector() : bool
- Detects if a specific request parameter is present.
- _setConfig() : void
- Process the config/settings data into properties.
- normalizeHeaderName() : string
- Normalize a header name into the SERVER version.
- processUrlOption() : array<string, mixed>
- Set environment vars based on `url` option to facilitate UriInterface instance generation.
- validateUploadedFiles() : void
- Recursively validate uploaded file data.
Properties
$trustProxy
Whether to trust HTTP_X headers set by most load balancers.
public
bool
$trustProxy
= false
Only set to true if your application runs behind load balancers/proxies that you control.
$_detectorCache
Instance cache for results of is(something) calls
protected
array<string, bool>
$_detectorCache
= []
$_detectors
The built in detectors used with `is()` can be modified with `addDetector()`.
protected
static array<string|int, callable|array<string|int, mixed>>
$_detectors
= ['get' => ['env' => 'REQUEST_METHOD', 'value' => 'GET'], 'post' => ['env' => 'REQUEST_METHOD', 'value' => 'POST'], 'put' => ['env' => 'REQUEST_METHOD', 'value' => 'PUT'], 'patch' => ['env' => 'REQUEST_METHOD', 'value' => 'PATCH'], 'delete' => ['env' => 'REQUEST_METHOD', 'value' => 'DELETE'], 'head' => ['env' => 'REQUEST_METHOD', 'value' => 'HEAD'], 'options' => ['env' => 'REQUEST_METHOD', 'value' => 'OPTIONS'], 'ssl' => ['env' => 'HTTPS', 'options' => [1, 'on']], 'ajax' => ['env' => 'HTTP_X_REQUESTED_WITH', 'value' => 'XMLHttpRequest'], 'json' => ['accept' => ['application/json'], 'param' => '_ext', 'value' => 'json'], 'xml' => ['accept' => ['application/xml', 'text/xml'], 'exclude' => ['text/html'], 'param' => '_ext', 'value' => 'xml']]
There are several ways to specify a detector, see \Cake\Http\ServerRequest::addDetector() for the various formats and ways to define detectors.
$_environment
Array of environment data.
protected
array<string, mixed>
$_environment
= []
$attributes
Store the additional attributes attached to the request.
protected
array<string, mixed>
$attributes
= []
$base
Base URL path.
protected
string
$base
$cookies
Array of cookie data.
protected
array<string, mixed>
$cookies
= []
$data
Array of POST data. Will contain form data as well as uploaded files.
protected
object|array<string|int, mixed>|null
$data
= []
In PUT/PATCH/DELETE requests this property will contain the form-urlencoded data.
$emulatedAttributes
A list of properties that emulated by the PSR7 attribute methods.
protected
array<string|int, string>
$emulatedAttributes
= ['session', 'flash', 'webroot', 'base', 'params', 'here']
$flash
Instance of a FlashMessage object relative to this request
protected
FlashMessage
$flash
$params
Array of parameters parsed from the URL.
protected
array<string|int, mixed>
$params
= ['plugin' => null, 'controller' => null, 'action' => null, '_ext' => null, 'pass' => []]
$protocol
The HTTP protocol version used.
protected
string|null
$protocol
$query
Array of query string arguments
protected
array<string|int, mixed>
$query
= []
$requestTarget
The request target if overridden
protected
string|null
$requestTarget
$session
Instance of a Session object relative to this request
protected
Session
$session
$stream
Request body stream. Contains php://input unless `input` constructor option is used.
protected
StreamInterface
$stream
$trustedProxies
Trusted proxies list
protected
array<string|int, string>
$trustedProxies
= []
$uploadedFiles
Array of Psr\Http\Message\UploadedFileInterface objects.
protected
array<string|int, mixed>
$uploadedFiles
= []
$uri
Uri instance
protected
UriInterface
$uri
$webroot
webroot path segment for the request.
protected
string
$webroot
= '/'
Methods
__call()
Missing method handler, handles wrapping older style isAjax() type methods
public
__call(string $name, array<string|int, mixed> $params) : bool
Parameters
- $name : string
-
The method called
- $params : array<string|int, mixed>
-
Array of parameters for the method call
Tags
Return values
bool__construct()
Create a new request object.
public
__construct([array<string, mixed> $config = [] ]) : mixed
You can supply the data as either an array or as a string. If you use a string you can only supply the URL for the request. Using an array will let you provide the following keys:
-
post
POST data or non query string data -
query
Additional data from the query string. -
files
Uploaded files in a normalized structure, with each leaf an instance of UploadedFileInterface. -
cookies
Cookies for this request. -
environment
$_SERVER and $_ENV data. -
url
The URL without the base path for the request. -
uri
The PSR7 UriInterface object. If null, one will be created fromurl
orenvironment
. -
base
The base URL for the request. -
webroot
The webroot directory for the request. -
input
The data that would come from php://input this is useful for simulating requests with put, patch or delete data. -
session
An instance of a Session object
Parameters
- $config : array<string, mixed> = []
-
An array of request data to create a request with.
acceptLanguage()
Get the languages accepted by the client, or check if a specific language is accepted.
public
acceptLanguage([string|null $language = null ]) : array<string|int, mixed>|bool
Get the list of accepted languages:
$request->acceptLanguage();
Check if a specific language is accepted:
$request->acceptLanguage('es-es');
Parameters
- $language : string|null = null
-
The language to test.
Return values
array<string|int, mixed>|bool —If a $language is provided, a boolean. Otherwise, the array of accepted languages.
accepts()
Find out which content types the client accepts or check if they accept a particular type of content.
public
accepts([string|null $type = null ]) : array<string|int, string>|bool
Get all types:
$this->request->accepts();
Check for a single type:
$this->request->accepts('application/json');
This method will order the returned content types by the preference values indicated by the client.
Parameters
- $type : string|null = null
-
The content type to check for. Leave null to get all types a client accepts.
Return values
array<string|int, string>|bool —Either an array of all the types the client accepts or a boolean if they accept the provided type.
addDetector()
Add a new detector to the list of detectors that a request can use.
public
static addDetector(string $name, callable|array<string|int, mixed> $detector) : void
There are several different types of detectors that can be set.
Callback comparison
Callback detectors allow you to provide a callable to handle the check. The callback will receive the request object as its only parameter.
addDetector('custom', function ($request) { //Return a boolean });
Environment value comparison
An environment value comparison, compares a value fetched from env()
to a known value
the environment value is equality checked against the provided value.
addDetector('post', ['env' => 'REQUEST_METHOD', 'value' => 'POST']);
Request parameter comparison
Allows for custom detectors on the request parameters.
addDetector('admin', ['param' => 'prefix', 'value' => 'admin']);
Accept comparison
Allows for detector to compare against Accept header value.
addDetector('csv', ['accept' => 'text/csv']);
Header comparison
Allows for one or more headers to be compared.
addDetector('fancy', ['header' => ['X-Fancy' => 1]);
The param
, env
and comparison types allow the following
value comparison options:
Pattern value comparison
Pattern value comparison allows you to compare a value fetched from env()
to a regular expression.
addDetector('iphone', ['env' => 'HTTP_USER_AGENT', 'pattern' => '/iPhone/i']);
Option based comparison
Option based comparisons use a list of options to create a regular expression. Subsequent calls to add an already defined options detector will merge the options.
addDetector('mobile', ['env' => 'HTTP_USER_AGENT', 'options' => ['Fennec']]);
You can also make compare against multiple values
using the options
key. This is useful when you want to check
if a request value is in a list of options.
addDetector('extension', ['param' => '_ext', 'options' => ['pdf', 'csv']]
Parameters
- $name : string
-
The name of the detector.
- $detector : callable|array<string|int, mixed>
-
A callable or options array for the detector definition.
allowMethod()
Allow only certain HTTP request methods, if the request method does not match a 405 error will be shown and the required "Allow" response header will be set.
public
allowMethod(array<string|int, string>|string $methods) : true
Example:
$this->request->allowMethod('post'); or $this->request->allowMethod(['post', 'delete']);
If the request would be GET, response header "Allow: POST, DELETE" will be set and a 405 error will be returned.
Parameters
- $methods : array<string|int, string>|string
-
Allowed HTTP request methods.
Tags
Return values
trueclearDetectorCache()
Clears the instance detector cache, used by the is() function
public
clearDetectorCache() : void
clientIp()
Get the IP the client is using, or says they are using.
public
clientIp() : string
Return values
string —The client IP.
contentType()
Get the content type used in this request.
public
contentType() : string|null
Return values
string|nulldomain()
Get the domain name and include $tldLength segments of the tld.
public
domain([int $tldLength = 1 ]) : string
Parameters
- $tldLength : int = 1
-
Number of segments your tld contains. For example:
example.com
contains 1 tld. Whileexample.co.uk
contains 2.
Return values
string —Domain name without subdomains.
getAttribute()
Read an attribute from the request, or get the default
public
getAttribute(string $name[, mixed|null $default = null ]) : mixed
Parameters
- $name : string
-
The attribute name.
- $default : mixed|null = null
-
The default value if the attribute has not been set.
getAttributes()
Get all the attributes in the request.
public
getAttributes() : array<string, mixed>
This will include the params, webroot, base, and here attributes that CakePHP provides.
Return values
array<string, mixed>getBody()
Gets the body of the message.
public
getBody() : StreamInterface
Return values
StreamInterface —Returns the body as a stream.
getCookie()
Read cookie data from the request's cookie data.
public
getCookie(string $key[, array<string|int, mixed>|string|null $default = null ]) : array<string|int, mixed>|string|null
Parameters
- $key : string
-
The key or dotted path you want to read.
- $default : array<string|int, mixed>|string|null = null
-
The default value if the cookie is not set.
Return values
array<string|int, mixed>|string|null —Either the cookie value, or null if the value doesn't exist.
getCookieCollection()
Get a cookie collection based on the request's cookies
public
getCookieCollection() : CookieCollection
The CookieCollection lets you interact with request cookies using
\Cake\Http\Cookie\Cookie
objects and can make converting request cookies
into response cookies easier.
This method will create a new cookie collection each time it is called.
This is an optimization that allows fewer objects to be allocated until
the more complex CookieCollection is needed. In general you should prefer
getCookie()
and getCookieParams()
over this method. Using a CookieCollection
is ideal if your cookies contain complex JSON encoded data.
Return values
CookieCollectiongetCookieParams()
Get all the cookie data from the request.
public
getCookieParams() : array<string, mixed>
Return values
array<string, mixed> —An array of cookie data.
getData()
Provides a safe accessor for request data. Allows you to use Hash::get() compatible paths.
public
getData([string|null $name = null ][, mixed $default = null ]) : mixed
Reading values.
// get all data
$request->getData();
// Read a specific field.
$request->getData('Post.title');
// With a default value.
$request->getData('Post.not there', 'default value');
When reading values you will get null
for keys/values that do not exist.
Developers are encouraged to use getParsedBody() if they need the whole data array, as it is PSR-7 compliant, and this method is not. Using Hash::get() you can also get single params.
PSR-7 Alternative
$value = Hash::get($request->getParsedBody(), 'Post.id');
Parameters
- $name : string|null = null
-
Dot separated name of the value to read. Or null to read all data.
- $default : mixed = null
-
The default data.
Return values
mixed —The value being read.
getEnv()
Get a value from the request's environment data.
public
getEnv(string $key[, string|null $default = null ]) : string|null
Fallback to using env() if the key is not set in the $environment property.
Parameters
- $key : string
-
The key you want to read from.
- $default : string|null = null
-
Default value when trying to retrieve an environment variable's value that does not exist.
Return values
string|null —Either the environment value, or null if the value doesn't exist.
getFlash()
Returns the instance of the FlashMessage object for this request
public
getFlash() : FlashMessage
Return values
FlashMessagegetHeader()
Get a single header from the request.
public
getHeader(string $name) : array<string|int, string>
Return the header value as an array. If the header is not present an empty array will be returned.
Parameters
- $name : string
-
The header you want to get (case-insensitive)
Tags
Return values
array<string|int, string> —An associative array of headers and their values. If the header doesn't exist, an empty array will be returned.
getHeaderLine()
Get a single header as a string from the request.
public
getHeaderLine(string $name) : string
Parameters
- $name : string
-
The header you want to get (case-insensitive)
Tags
Return values
string —Header values collapsed into a comma separated string.
getHeaders()
Get all headers in the request.
public
getHeaders() : array<string|int, array<string|int, string>>
Returns an associative array where the header names are the keys and the values are a list of header values.
While header names are not case-sensitive, getHeaders() will normalize the headers.
Tags
Return values
array<string|int, array<string|int, string>> —An associative array of headers and their values.
getMethod()
Get the HTTP method used for this request.
public
getMethod() : string
There are a few ways to specify a method.
- If your client supports it you can use native HTTP methods.
- You can set the HTTP-X-Method-Override header.
- You can submit an input with the name
_method
Any of these 3 approaches can be used to set the HTTP method used by CakePHP internally, and will effect the result of this method.
Tags
Return values
string —The name of the HTTP method used.
getParam()
Safely access the values in $this->params.
public
getParam(string $name[, mixed $default = null ]) : mixed
Parameters
- $name : string
-
The name or dotted path to parameter.
- $default : mixed = null
-
The default value if
$name
is not set. Defaultnull
.
getParsedBody()
Get the parsed request body data.
public
getParsedBody() : object|array<string|int, mixed>|null
If the request Content-Type is either application/x-www-form-urlencoded or multipart/form-data, and the request method is POST, this will be the post data. For other content types, it may be the deserialized request body.
Return values
object|array<string|int, mixed>|null —The deserialized body parameters, if any. These will typically be an array.
getPath()
Get the path of current request.
public
getPath() : string
Tags
Return values
stringgetProtocolVersion()
Retrieves the HTTP protocol version as a string.
public
getProtocolVersion() : string
Return values
string —HTTP protocol version.
getQuery()
Read a specific query value or dotted path.
public
getQuery([string|null $name = null ][, mixed $default = null ]) : array<string|int, mixed>|string|null
Developers are encouraged to use getQueryParams() if they need the whole query array, as it is PSR-7 compliant, and this method is not. Using Hash::get() you can also get single params.
PSR-7 Alternative
$value = Hash::get($request->getQueryParams(), 'Post.id');
Parameters
- $name : string|null = null
-
The name or dotted path to the query param or null to read all.
- $default : mixed = null
-
The default value if the named parameter is not set, and $name is not null.
Tags
Return values
array<string|int, mixed>|string|null —Query data.
getQueryParams()
Get all the query parameters in accordance to the PSR-7 specifications. To read specific query values use the alternative getQuery() method.
public
getQueryParams() : array<string|int, mixed>
Tags
Return values
array<string|int, mixed>getRequestTarget()
Retrieves the request's target.
public
getRequestTarget() : string
Retrieves the message's request-target either as it was requested,
or as set with withRequestTarget()
. By default this will return the
application relative path without base directory, and the query string
defined in the SERVER environment.
Return values
stringgetServerParams()
Get all the server environment parameters.
public
getServerParams() : array<string|int, mixed>
Read all of the 'environment' or 'server' data that was used to create this request.
Tags
Return values
array<string|int, mixed>getSession()
Returns the instance of the Session object for this request
public
getSession() : Session
Return values
SessiongetTrustedProxies()
Get trusted proxies
public
getTrustedProxies() : array<string|int, string>
Return values
array<string|int, string>getUploadedFile()
Get the uploaded file from a dotted path.
public
getUploadedFile(string $path) : UploadedFileInterface|null
Parameters
- $path : string
-
The dot separated path to the file you want.
Return values
UploadedFileInterface|nullgetUploadedFiles()
Get the array of uploaded files from the request.
public
getUploadedFiles() : array<string|int, mixed>
Return values
array<string|int, mixed>getUri()
Retrieves the URI instance.
public
getUri() : UriInterface
Return values
UriInterface —Returns a UriInterface instance representing the URI of the request.
hasHeader()
Check if a header is set in the request.
public
hasHeader(string $name) : bool
Parameters
- $name : string
-
The header you want to get (case-insensitive)
Tags
Return values
bool —Whether the header is defined.
host()
Get the host that the request was handled on.
public
host() : string|null
Return values
string|nullinput()
Read data from `php://input`. Useful when interacting with XML or JSON request body content.
public
input([callable|null $callback = null ], mixed ...$args) : mixed
Use (string)$request->getBody()
to get the raw PHP input
as string; use BodyParserMiddleware
to parse the request body so that it's
available as array/object through $request->getParsedBody()
.
Getting input with a decoding function:
$this->request->input('json_decode');
Getting input using a decoding function, and additional params:
$this->request->input('Xml::build', ['return' => 'DOMDocument']);
Any additional parameters are applied to the callback in the order they are given.
Parameters
- $callback : callable|null = null
-
A decoding callback that will convert the string data to another representation. Leave empty to access the raw input data. You can also supply additional parameters for the decoding callback using var args, see above.
- $args : mixed
-
The additional arguments
Return values
mixed —The decoded/processed request data.
is()
Check whether a Request is a certain type.
public
is(array<string|int, string>|string $type, mixed ...$args) : bool
Uses the built-in detection rules as well as additional rules
defined with . Any detector can be called
as is($type)
or is$Type()
.
Parameters
- $type : array<string|int, string>|string
-
The type of request you want to check. If an array this method will return true if the request matches any type.
- $args : mixed
-
List of arguments
Return values
bool —Whether the request is the type you are checking.
isAll()
Check that a request matches all the given types.
public
isAll(array<string|int, string> $types) : bool
Allows you to test multiple types and union the results. See Request::is() for how to add additional types and the built-in types.
Parameters
- $types : array<string|int, string>
-
The types to check.
Tags
Return values
bool —Success.
parseAccept()
Parse the HTTP_ACCEPT header and return a sorted array with content types as the keys, and pref values as the values.
public
parseAccept() : array<string|int, mixed>
Use accepts()
or ContentTypeNegotiation
class instead.
Generally you want to use to get a simple list of the accepted content types.
Return values
array<string|int, mixed> —An array of prefValue => [content/types]
port()
Get the port the request was handled on.
public
port() : string|null
Return values
string|nullreferer()
Returns the referer that referred this request.
public
referer([bool $local = true ]) : string|null
Parameters
- $local : bool = true
-
Attempt to return a local address. Local addresses do not contain hostnames.
Return values
string|null —The referring address for this request or null.
scheme()
Get the current url scheme used for the request.
public
scheme() : string|null
e.g. 'http', or 'https'
Return values
string|null —The scheme used for the request.
setTrustedProxies()
register trusted proxies
public
setTrustedProxies(array<string|int, string> $proxies) : void
Parameters
- $proxies : array<string|int, string>
-
ips list of trusted proxies
subdomains()
Get the subdomains for a host.
public
subdomains([int $tldLength = 1 ]) : array<string|int, string>
Parameters
- $tldLength : int = 1
-
Number of segments your tld contains. For example:
example.com
contains 1 tld. Whileexample.co.uk
contains 2.
Return values
array<string|int, string> —An array of subdomains.
withAddedHeader()
Get a modified request with the provided header.
public
withAddedHeader(string $name, array<string|int, mixed>|string $value) : static
Existing header values will be retained. The provided value will be appended into the existing values.
Parameters
- $name : string
-
The header name.
- $value : array<string|int, mixed>|string
-
The header value
Tags
Return values
staticwithAttribute()
Return an instance with the specified request attribute.
public
withAttribute(string $name, mixed $value) : static
Parameters
- $name : string
-
The attribute name.
- $value : mixed
-
The value of the attribute.
Return values
staticwithBody()
Return an instance with the specified message body.
public
withBody(StreamInterface $body) : static
Parameters
- $body : StreamInterface
-
The new request body
Return values
staticwithCookieCollection()
Replace the cookies in the request with those contained in the provided CookieCollection.
public
withCookieCollection(CookieCollection $cookies) : static
Parameters
- $cookies : CookieCollection
-
The cookie collection
Return values
staticwithCookieParams()
Replace the cookies and get a new request instance.
public
withCookieParams(array<string|int, mixed> $cookies) : static
Parameters
- $cookies : array<string|int, mixed>
-
The new cookie data to use.
Return values
staticwithData()
Update the request with a new request data element.
public
withData(string $name, mixed $value) : static
Returns an updated request object. This method returns a new request object and does not mutate the request in-place.
Use withParsedBody()
if you need to replace the all request data.
Parameters
- $name : string
-
The dot separated path to insert $value at.
- $value : mixed
-
The value to insert into the request data.
Return values
staticwithEnv()
Update the request with a new environment data element.
public
withEnv(string $key, string $value) : static
Returns an updated request object. This method returns a new request object and does not mutate the request in-place.
Parameters
- $key : string
-
The key you want to write to.
- $value : string
-
Value to set
Return values
staticwithHeader()
Get a modified request with the provided header.
public
withHeader(string $name, array<string|int, mixed>|string $value) : static
Parameters
- $name : string
-
The header name.
- $value : array<string|int, mixed>|string
-
The header value
Tags
Return values
staticwithMethod()
Update the request method and get a new instance.
public
withMethod(string $method) : static
Parameters
- $method : string
-
The HTTP method to use.
Tags
Return values
static —A new instance with the updated method.
withoutAttribute()
Return an instance without the specified request attribute.
public
withoutAttribute(string $name) : static
Parameters
- $name : string
-
The attribute name.
Tags
Return values
staticwithoutData()
Update the request removing a data element.
public
withoutData(string $name) : static
Returns an updated request object. This method returns a new request object and does not mutate the request in-place.
Parameters
- $name : string
-
The dot separated path to remove.
Return values
staticwithoutHeader()
Get a modified request without a provided header.
public
withoutHeader(string $name) : static
Parameters
- $name : string
-
The header name to remove.
Tags
Return values
staticwithParam()
Update the request with a new routing parameter
public
withParam(string $name, mixed $value) : static
Returns an updated request object. This method returns a new request object and does not mutate the request in-place.
Parameters
- $name : string
-
The dot separated path to insert $value at.
- $value : mixed
-
The value to insert into the the request parameters.
Return values
staticwithParsedBody()
Update the parsed body and get a new instance.
public
withParsedBody(object|array<string|int, mixed>|null $data) : static
Parameters
- $data : object|array<string|int, mixed>|null
-
The deserialized body data. This will typically be in an array or object.
Return values
staticwithProtocolVersion()
Return an instance with the specified HTTP protocol version.
public
withProtocolVersion(string $version) : static
The version string MUST contain only the HTTP version number (e.g., "1.1", "1.0").
Parameters
- $version : string
-
HTTP protocol version
Return values
staticwithQueryParams()
Update the query string data and get a new instance.
public
withQueryParams(array<string|int, mixed> $query) : static
Parameters
- $query : array<string|int, mixed>
-
The query string data to use
Tags
Return values
static —A new instance with the updated query string data.
withRequestTarget()
Create a new instance with a specific request-target.
public
withRequestTarget(string $requestTarget) : static
You can use this method to overwrite the request target that is inferred from the request's Uri. This also lets you change the request target's form to an absolute-form, authority-form or asterisk-form
Parameters
- $requestTarget : string
-
The request target.
Tags
Return values
staticwithUploadedFiles()
Update the request replacing the files, and creating a new instance.
public
withUploadedFiles(array<string|int, mixed> $uploadedFiles) : static
Parameters
- $uploadedFiles : array<string|int, mixed>
-
An array of uploaded file objects.
Tags
Return values
staticwithUri()
Return an instance with the specified uri
public
withUri(UriInterface $uri[, bool $preserveHost = false ]) : static
Warning Replacing the Uri will not update the base
, webroot
,
and url
attributes.
Parameters
- $uri : UriInterface
-
The new request uri
- $preserveHost : bool = false
-
Whether the host should be retained.
Return values
static_acceptHeaderDetector()
Detects if a specific accept header is present.
protected
_acceptHeaderDetector(array<string|int, mixed> $detect) : bool
Parameters
- $detect : array<string|int, mixed>
-
Detector options array.
Return values
bool —Whether the request is the type you are checking.
_environmentDetector()
Detects if a specific environment variable is present.
protected
_environmentDetector(array<string|int, mixed> $detect) : bool
Parameters
- $detect : array<string|int, mixed>
-
Detector options array.
Return values
bool —Whether the request is the type you are checking.
_headerDetector()
Detects if a specific header is present.
protected
_headerDetector(array<string|int, mixed> $detect) : bool
Parameters
- $detect : array<string|int, mixed>
-
Detector options array.
Return values
bool —Whether the request is the type you are checking.
_is()
Worker for the public is() function
protected
_is(string $type, array<string|int, mixed> $args) : bool
Parameters
- $type : string
-
The type of request you want to check.
- $args : array<string|int, mixed>
-
Array of custom detector arguments.
Return values
bool —Whether the request is the type you are checking.
_paramDetector()
Detects if a specific request parameter is present.
protected
_paramDetector(array<string|int, mixed> $detect) : bool
Parameters
- $detect : array<string|int, mixed>
-
Detector options array.
Return values
bool —Whether the request is the type you are checking.
_setConfig()
Process the config/settings data into properties.
protected
_setConfig(array<string, mixed> $config) : void
Parameters
- $config : array<string, mixed>
-
The config data to use.
normalizeHeaderName()
Normalize a header name into the SERVER version.
protected
normalizeHeaderName(string $name) : string
Parameters
- $name : string
-
The header name.
Return values
string —The normalized header name.
processUrlOption()
Set environment vars based on `url` option to facilitate UriInterface instance generation.
protected
processUrlOption(array<string, mixed> $config) : array<string, mixed>
query
option is also updated based on URL's querystring.
Parameters
- $config : array<string, mixed>
-
Config array.
Return values
array<string, mixed> —Update config.
validateUploadedFiles()
Recursively validate uploaded file data.
protected
validateUploadedFiles(array<string|int, mixed> $uploadedFiles, string $path) : void
Parameters
- $uploadedFiles : array<string|int, mixed>
-
The new files array to validate.
- $path : string
-
The path thus far.