class Request

Request represents an HTTP request.

The methods dealing with URL accept / return a raw path (% encoded): * getBasePath * getBaseUrl * getPathInfo * getRequestUri * getUri * getUriForPath

Constants

HEADER_FORWARDED

HEADER_X_FORWARDED_FOR

HEADER_X_FORWARDED_HOST

HEADER_X_FORWARDED_PROTO

HEADER_X_FORWARDED_PORT

HEADER_X_FORWARDED_ALL

HEADER_X_FORWARDED_AWS_ELB

METHOD_HEAD

METHOD_GET

METHOD_POST

METHOD_PUT

METHOD_PATCH

METHOD_DELETE

METHOD_PURGE

METHOD_OPTIONS

METHOD_TRACE

METHOD_CONNECT

Properties

static protected string[] $trustedProxies
static protected string[] $trustedHostPatterns
static protected string[] $trustedHosts
static protected $httpMethodParameterOverride
ParameterBag $attributes Custom parameters.
ParameterBag $request Request body parameters ($_POST).
ParameterBag $query Query string parameters ($_GET).
ServerBag $server Server and execution environment parameters ($_SERVER).
FileBag $files Uploaded files ($_FILES).
ParameterBag $cookies Cookies ($_COOKIE).
HeaderBag $headers Headers (taken from the $_SERVER).
protected string|resource|false|null $content
protected array $languages
protected array $charsets
protected array $encodings
protected array $acceptableContentTypes
protected string $pathInfo
protected string $requestUri
protected string $baseUrl
protected string $basePath
protected string $method
protected string $format
protected SessionInterface $session
protected string $locale
protected string $defaultLocale
static protected array $formats
static protected $requestFactory

Methods

__construct(array $query = array(), array $request = array(), array $attributes = array(), array $cookies = array(), array $files = array(), array $server = array(), string|resource|null $content = null)

No description

initialize(array $query = array(), array $request = array(), array $attributes = array(), array $cookies = array(), array $files = array(), array $server = array(), string|resource|null $content = null)

Sets the parameters for this request.

static Request
createFromGlobals()

Creates a new request with values from PHP's super globals.

static Request
create(string $uri, string $method = 'GET', array $parameters = array(), array $cookies = array(), array $files = array(), array $server = array(), string|resource|null $content = null)

Creates a Request based on a given URI and configuration.

static 
setFactory(callable|null $callable)

Sets a callable able to create a Request instance.

duplicate(array $query = null, array $request = null, array $attributes = null, array $cookies = null, array $files = null, array $server = null)

Clones a request and overrides some of its parameters.

__clone()

Clones the current request.

string
__toString()

Returns the request as a string.

overrideGlobals()

Overrides the PHP global variables according to this request instance.

static 
setTrustedProxies(array $proxies, int $trustedHeaderSet)

Sets a list of trusted proxies.

static array
getTrustedProxies()

Gets the list of trusted proxies.

static int
getTrustedHeaderSet()

Gets the set of trusted headers from trusted proxies.

static 
setTrustedHosts(array $hostPatterns)

Sets a list of trusted host patterns.

static array
getTrustedHosts()

Gets the list of trusted host patterns.

static string
normalizeQueryString(string $qs)

Normalizes a query string.

static 
enableHttpMethodParameterOverride()

Enables support for the _method request parameter to determine the intended HTTP method.

static bool
getHttpMethodParameterOverride()

Checks whether support for the _method request parameter is enabled.

mixed
get(string $key, mixed $default = null)

Gets a "parameter" value from any bag.

getSession()

Gets the Session.

bool
hasPreviousSession()

Whether the request contains a Session which was started in one of the previous requests.

bool
hasSession()

Whether the request contains a Session object.

setSession(SessionInterface $session)

Sets the Session.

setSessionFactory(callable $factory)

No description

array
getClientIps()

Returns the client IP addresses.

string|null
getClientIp()

Returns the client IP address.

string
getScriptName()

Returns current script name.

string
getPathInfo()

Returns the path being requested relative to the executed script.

string
getBasePath()

Returns the root path from which this request is executed.

string
getBaseUrl()

Returns the root URL from which this request is executed.

string
getScheme()

Gets the request's scheme.

int|string
getPort()

Returns the port on which the request is made.

string|null
getUser()

Returns the user.

string|null
getPassword()

Returns the password.

string
getUserInfo()

Gets the user info.

string
getHttpHost()

Returns the HTTP host being requested.

string
getRequestUri()

Returns the requested URI (path and query string).

string
getSchemeAndHttpHost()

Gets the scheme and HTTP host.

string
getUri()

Generates a normalized URI (URL) for the Request.

string
getUriForPath(string $path)

Generates a normalized URI for the given path.

string
getRelativeUriForPath(string $path)

Returns the path as relative reference from the current Request path.

string|null
getQueryString()

Generates the normalized query string for the Request.

bool
isSecure()

Checks whether the request is secure or not.

string
getHost()

Returns the host name.

setMethod(string $method)

Sets the request method.

string
getMethod()

Gets the request "intended" method.

string
getRealMethod()

Gets the "real" request method.

string|null
getMimeType(string $format)

Gets the mime type associated with the format.

static array
getMimeTypes(string $format)

Gets the mime types associated with the format.

string|null
getFormat(string $mimeType)

Gets the format associated with the mime type.

setFormat(string $format, string|array $mimeTypes)

Associates a format with mime types.

string
getRequestFormat(string|null $default = 'html')

Gets the request format.

setRequestFormat(string $format)

Sets the request format.

string|null
getContentType()

Gets the format associated with the request.

setDefaultLocale(string $locale)

Sets the default locale.

string
getDefaultLocale()

Get the default locale.

setLocale(string $locale)

Sets the locale.

string
getLocale()

Get the locale.

bool
isMethod(string $method)

Checks if the request method is of specified type.

bool
isMethodSafe()

Checks whether or not the method is safe.

bool
isMethodIdempotent()

Checks whether or not the method is idempotent.

bool
isMethodCacheable()

Checks whether the method is cacheable or not.

string
getProtocolVersion()

Returns the protocol version.

string|resource
getContent(bool $asResource = false)

Returns the request body content.

array
getETags()

Gets the Etags.

bool
isNoCache()

No description

string|null
getPreferredLanguage(array $locales = null)

Returns the preferred language.

array
getLanguages()

Gets a list of languages acceptable by the client browser.

array
getCharsets()

Gets a list of charsets acceptable by the client browser.

array
getEncodings()

Gets a list of encodings acceptable by the client browser.

array
getAcceptableContentTypes()

Gets a list of content types acceptable by the client browser.

bool
isXmlHttpRequest()

Returns true if the request is a XMLHttpRequest.

prepareRequestUri()

No description

string
prepareBaseUrl()

Prepares the base URL.

string
prepareBasePath()

Prepares the base path.

string
preparePathInfo()

Prepares the path info.

static 
initializeFormats()

Initializes HTTP request formats.

bool
isFromTrustedProxy()

Indicates whether this request originated from a trusted proxy.

Details

at line 233
__construct(array $query = array(), array $request = array(), array $attributes = array(), array $cookies = array(), array $files = array(), array $server = array(), string|resource|null $content = null)

Parameters

array $query The GET parameters
array $request The POST parameters
array $attributes The request attributes (parameters parsed from the PATH_INFO, ...)
array $cookies The COOKIE parameters
array $files The FILES parameters
array $server The SERVER parameters
string|resource|null $content The raw body data

at line 251
initialize(array $query = array(), array $request = array(), array $attributes = array(), array $cookies = array(), array $files = array(), array $server = array(), string|resource|null $content = null)

Sets the parameters for this request.

This method also re-initializes all properties.

Parameters

array $query The GET parameters
array $request The POST parameters
array $attributes The request attributes (parameters parsed from the PATH_INFO, ...)
array $cookies The COOKIE parameters
array $files The FILES parameters
array $server The SERVER parameters
string|resource|null $content The raw body data

at line 279
static Request createFromGlobals()

Creates a new request with values from PHP's super globals.

Return Value

Request

at line 309
static Request create(string $uri, string $method = 'GET', array $parameters = array(), array $cookies = array(), array $files = array(), array $server = array(), string|resource|null $content = null)

Creates a Request based on a given URI and configuration.

The information contained in the URI always take precedence over the other information (server and parameters).

Parameters

string $uri The URI
string $method The HTTP method
array $parameters The query (GET) or request (POST) parameters
array $cookies The request cookies ($_COOKIE)
array $files The request files ($_FILES)
array $server The server parameters ($_SERVER)
string|resource|null $content The raw body data

Return Value

Request

at line 410
static setFactory(callable|null $callable)

Sets a callable able to create a Request instance.

This is mainly useful when you need to override the Request class to keep BC with an existing system. It should not be used for any other purpose.

Parameters

callable|null $callable A PHP callable

at line 427
Request duplicate(array $query = null, array $request = null, array $attributes = null, array $cookies = null, array $files = null, array $server = null)

Clones a request and overrides some of its parameters.

Parameters

array $query The GET parameters
array $request The POST parameters
array $attributes The request attributes (parameters parsed from the PATH_INFO, ...)
array $cookies The COOKIE parameters
array $files The FILES parameters
array $server The SERVER parameters

Return Value

Request

at line 477
__clone()

Clones the current request.

Note that the session is not cloned as duplicated requests are most of the time sub-requests of the main one.

at line 493
string __toString()

Returns the request as a string.

Return Value

string The request

at line 525
overrideGlobals()

Overrides the PHP global variables according to this request instance.

It overrides $_GET, $_POST, $_REQUEST, $_SERVER, $_COOKIE. $_FILES is never overridden, see rfc1867

at line 567
static setTrustedProxies(array $proxies, int $trustedHeaderSet)

Sets a list of trusted proxies.

You should only list the reverse proxies that you manage directly.

Parameters

array $proxies A list of trusted proxies
int $trustedHeaderSet A bit field of Request::HEADER_*, to set which headers to trust from your proxies

Exceptions

InvalidArgumentException When $trustedHeaderSet is invalid

at line 578
static array getTrustedProxies()

Gets the list of trusted proxies.

Return Value

array An array of trusted proxies

at line 588
static int getTrustedHeaderSet()

Gets the set of trusted headers from trusted proxies.

Return Value

int A bit field of Request::HEADER_* that defines which headers are trusted from your proxies

at line 600
static setTrustedHosts(array $hostPatterns)

Sets a list of trusted host patterns.

You should only list the hosts you manage using regexs.

Parameters

array $hostPatterns A list of trusted host patterns

at line 614
static array getTrustedHosts()

Gets the list of trusted host patterns.

Return Value

array An array of trusted host patterns

at line 629
static string normalizeQueryString(string $qs)

Normalizes a query string.

It builds a normalized query string, where keys/value pairs are alphabetized, have consistent escaping and unneeded delimiters are removed.

Parameters

string $qs Query string

Return Value

string A normalized query string for the Request

at line 652
static enableHttpMethodParameterOverride()

Enables support for the _method request parameter to determine the intended HTTP method.

Be warned that enabling this feature might lead to CSRF issues in your code. Check that you are using CSRF tokens when required. If the HTTP method parameter override is enabled, an html-form with method "POST" can be altered and used to send a "PUT" or "DELETE" request via the _method request parameter. If these methods are not protected against CSRF, this presents a possible vulnerability.

The HTTP method can only be overridden when the real HTTP method is POST.

at line 662
static bool getHttpMethodParameterOverride()

Checks whether support for the _method request parameter is enabled.

Return Value

bool True when the _method request parameter is enabled, false otherwise

at line 681
mixed get(string $key, mixed $default = null)

Gets a "parameter" value from any bag.

This method is mainly useful for libraries that want to provide some flexibility. If you don't need the flexibility in controllers, it is better to explicitly get request parameters from the appropriate public property instead (attributes, query, request).

Order of precedence: PATH (routing placeholders or custom attributes), GET, BODY

Parameters

string $key The key
mixed $default The default value if the parameter key does not exist

Return Value

mixed

at line 703
SessionInterface|null getSession()

Gets the Session.

Return Value

SessionInterface|null The session

at line 724
bool hasPreviousSession()

Whether the request contains a Session which was started in one of the previous requests.

Return Value

bool

at line 739
bool hasSession()

Whether the request contains a Session object.

This method does not give any information about the state of the session object, like whether the session is started or not. It is just a way to check if this Request is associated with a Session instance.

Return Value

bool true when the Request contains a Session object, false otherwise

at line 749
setSession(SessionInterface $session)

Sets the Session.

Parameters

SessionInterface $session The Session

at line 757
setSessionFactory(callable $factory)

Parameters

callable $factory

at line 775
array getClientIps()

Returns the client IP addresses.

In the returned array the most trusted IP address is first, and the least trusted one last. The "real" client IP address is the last one, but this is also the least trusted one. Trusted proxies are stripped.

Use this method carefully; you should use getClientIp() instead.

Return Value

array The client IP addresses

See also

getClientIp()

at line 800
string|null getClientIp()

Returns the client IP address.

This method can read the client IP address from the "X-Forwarded-For" header when trusted proxies were set via "setTrustedProxies()". The "X-Forwarded-For" header value is a comma+space separated list of IP addresses, the left-most being the original client, and each successive proxy that passed the request adding the IP address where it received the request from.

Return Value

string|null The client IP address

See also

getClientIps()
http://en.wikipedia.org/wiki/X-Forwarded-For

at line 812
string getScriptName()

Returns current script name.

Return Value

string

at line 831
string getPathInfo()

Returns the path being requested relative to the executed script.

The path info always starts with a /.

Suppose this request is instantiated from /mysite on localhost:

  • http://localhost/mysite returns an empty string
  • http://localhost/mysite/about returns '/about'
  • http://localhost/mysite/enco%20ded returns '/enco%20ded'
  • http://localhost/mysite/about?var=1 returns '/about'

Return Value

string The raw path (i.e. not urldecoded)

at line 852
string getBasePath()

Returns the root path from which this request is executed.

Suppose that an index.php file instantiates this request object:

  • http://localhost/index.php returns an empty string
  • http://localhost/index.php/page returns an empty string
  • http://localhost/web/index.php returns '/web'
  • http://localhost/we%20b/index.php returns '/we%20b'

Return Value

string The raw path (i.e. not urldecoded)

at line 871
string getBaseUrl()

Returns the root URL from which this request is executed.

The base URL never ends with a /.

This is similar to getBasePath(), except that it also includes the script filename (e.g. index.php) if one exists.

Return Value

string The raw URL (i.e. not urldecoded)

at line 885
string getScheme()

Gets the request's scheme.

Return Value

string

at line 900
int|string getPort()

Returns the port on which the request is made.

This method can read the client port from the "X-Forwarded-Port" header when trusted proxies were set via "setTrustedProxies()".

The "X-Forwarded-Port" header must contain the client port.

Return Value

int|string can be a string if fetched from the server bag

at line 928
string|null getUser()

Returns the user.

Return Value

string|null

at line 938
string|null getPassword()

Returns the password.

Return Value

string|null

at line 948
string getUserInfo()

Gets the user info.

Return Value

string A user name and, optionally, scheme-specific information about how to gain authorization to access the server

at line 967
string getHttpHost()

Returns the HTTP host being requested.

The port name will be appended to the host if it's non-standard.

Return Value

string

at line 984
string getRequestUri()

Returns the requested URI (path and query string).

Return Value

string The raw URI (i.e. not URI decoded)

at line 1001
string getSchemeAndHttpHost()

Gets the scheme and HTTP host.

If the URL was called with basic authentication, the user and the password are not added to the generated string.

Return Value

string The scheme and HTTP host

at line 1013
string getUri()

Generates a normalized URI (URL) for the Request.

Return Value

string A normalized URI (URL) for the Request

See also

getQueryString()

at line 1029
string getUriForPath(string $path)

Generates a normalized URI for the given path.

Parameters

string $path A path to use instead of the current one

Return Value

string The normalized URI for the path

at line 1053
string getRelativeUriForPath(string $path)

Returns the path as relative reference from the current Request path.

Only the URIs path component (no schema, host etc.) is relevant and must be given. Both paths must be absolute and not contain relative parts. Relative URLs from one resource to another are useful when generating self-contained downloadable document archives. Furthermore, they can be used to reduce the link size in documents.

Example target paths, given a base path of "/a/b/c/d": - "/a/b/c/d" -> "" - "/a/b/c/" -> "./" - "/a/b/" -> "../" - "/a/b/c/other" -> "other" - "/a/x/y" -> "../../x/y"

Parameters

string $path The target path

Return Value

string The relative target path

at line 1097
string|null getQueryString()

Generates the normalized query string for the Request.

It builds a normalized query string, where keys/value pairs are alphabetized and have consistent escaping.

Return Value

string|null A normalized query string for the Request

at line 1114
bool isSecure()

Checks whether the request is secure or not.

This method can read the client protocol from the "X-Forwarded-Proto" header when trusted proxies were set via "setTrustedProxies()".

The "X-Forwarded-Proto" header must contain the protocol: "https" or "http".

Return Value

bool

at line 1137
string getHost()

Returns the host name.

This method can read the client host name from the "X-Forwarded-Host" header when trusted proxies were set via "setTrustedProxies()".

The "X-Forwarded-Host" header must contain the client host name.

Return Value

string

Exceptions

SuspiciousOperationException when the host name is invalid or not trusted

at line 1194
setMethod(string $method)

Sets the request method.

Parameters

string $method

at line 1215
string getMethod()

Gets the request "intended" method.

If the X-HTTP-Method-Override header is set, and if the method is a POST, then it is used to determine the "real" intended HTTP method.

The _method request parameter can also be used to determine the HTTP method, but only if enableHttpMethodParameterOverride() has been called.

The method is always an uppercased string.

Return Value

string The request method

See also

getRealMethod()

at line 1242
string getRealMethod()

Gets the "real" request method.

Return Value

string The request method

See also

getMethod()

at line 1254
string|null getMimeType(string $format)

Gets the mime type associated with the format.

Parameters

string $format The format

Return Value

string|null The associated mime type (null if not found)

at line 1270
static array getMimeTypes(string $format)

Gets the mime types associated with the format.

Parameters

string $format The format

Return Value

array The associated mime types

at line 1286
string|null getFormat(string $mimeType)

Gets the format associated with the mime type.

Parameters

string $mimeType The associated mime type

Return Value

string|null The format (null if not found)

at line 1313
setFormat(string $format, string|array $mimeTypes)

Associates a format with mime types.

Parameters

string $format The format
string|array $mimeTypes The associated mime types (the preferred one must be the first as it will be used as the content type)

at line 1335
string getRequestFormat(string|null $default = 'html')

Gets the request format.

Here is the process to determine the format:

  • format defined by the user (with setRequestFormat())
  • _format request attribute
  • $default

Parameters

string|null $default The default format

Return Value

string The request format

at line 1349
setRequestFormat(string $format)

Sets the request format.

Parameters

string $format The request format

at line 1359
string|null getContentType()

Gets the format associated with the request.

Return Value

string|null The format (null if no content type is present)

at line 1369
setDefaultLocale(string $locale)

Sets the default locale.

Parameters

string $locale

at line 1383
string getDefaultLocale()

Get the default locale.

Return Value

string

at line 1393
setLocale(string $locale)

Sets the locale.

Parameters

string $locale

at line 1403
string getLocale()

Get the locale.

Return Value

string

at line 1415
bool isMethod(string $method)

Checks if the request method is of specified type.

Parameters

string $method Uppercase request method (GET, POST etc)

Return Value

bool

at line 1429
bool isMethodSafe()

Checks whether or not the method is safe.

at line 1444
bool isMethodIdempotent()

Checks whether or not the method is idempotent.

Return Value

bool

at line 1456
bool isMethodCacheable()

Checks whether the method is cacheable or not.

at line 1472
string getProtocolVersion()

Returns the protocol version.

If the application is behind a proxy, the protocol version used in the requests between the client and the proxy and between the proxy and the server might be different. This returns the former (from the "Via" header) if the proxy is trusted (see "setTrustedProxies()"), otherwise it returns the latter (from the "SERVER_PROTOCOL" server parameter).

Return Value

string

at line 1494
string|resource getContent(bool $asResource = false)

Returns the request body content.

Parameters

bool $asResource If true, a resource will be returned

Return Value

string|resource The request body content or a resource to read the body stream

Exceptions

LogicException

at line 1537
array getETags()

Gets the Etags.

Return Value

array The entity tags

at line 1545
bool isNoCache()

Return Value

bool

at line 1557
string|null getPreferredLanguage(array $locales = null)

Returns the preferred language.

Parameters

array $locales An array of ordered available locales

Return Value

string|null The preferred locale

at line 1590
array getLanguages()

Gets a list of languages acceptable by the client browser.

Return Value

array Languages ordered in the user browser preferences

at line 1630
array getCharsets()

Gets a list of charsets acceptable by the client browser.

Return Value

array List of charsets in preferable order

at line 1644
array getEncodings()

Gets a list of encodings acceptable by the client browser.

Return Value

array List of encodings in preferable order

at line 1658
array getAcceptableContentTypes()

Gets a list of content types acceptable by the client browser.

Return Value

array List of content types in preferable order

at line 1677
bool isXmlHttpRequest()

Returns true if the request is a XMLHttpRequest.

It works if your JavaScript library sets an X-Requested-With HTTP header. It is known to work with common JavaScript frameworks:

Return Value

bool true if the request is an XMLHttpRequest, false otherwise

See also

http://en.wikipedia.org/wiki/List_of_Ajax_frameworks#JavaScript

at line 1690
protected prepareRequestUri()

at line 1726
protected string prepareBaseUrl()

Prepares the base URL.

Return Value

string

at line 1795
protected string prepareBasePath()

Prepares the base path.

Return Value

string base path

at line 1821
protected string preparePathInfo()

Prepares the path info.

Return Value

string path info

at line 1851
static protected initializeFormats()

Initializes HTTP request formats.

at line 1925
bool isFromTrustedProxy()

Indicates whether this request originated from a trusted proxy.

This can be useful to determine whether or not to trust the contents of a proxy-specific header.

Return Value

bool true if the request came from a trusted proxy, false otherwise