Class

Symfony\Component\HttpFoundation\Request

class Request

Request represents an HTTP request.

Constants

HEADER_CLIENT_IP

HEADER_CLIENT_HOST

HEADER_CLIENT_PROTO

HEADER_CLIENT_PORT

Properties

ParameterBag $attributes
ParameterBag $request
ParameterBag $query
ServerBag $server
FileBag $files
ParameterBag $cookies
HeaderBag $headers

Methods

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

Constructor.

initialize(array $query = array(), array $request = array(), array $attributes = array(), array $cookies = array(), array $files = array(), array $server = array(), string $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 $content = null)

Creates a Request based on a given URI and configuration.

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  trustProxyData()

Trusts $_SERVER entries coming from proxies.

static  setTrustedProxies(array $proxies)

Sets a list of trusted proxies.

static  setTrustedHosts(array $hostPatterns)

Sets a list of trusted host patterns.

static array getTrustedHosts()

Gets the list of trusted host patterns.

static  setTrustedHeaderName(string $key, string $value)

Sets the name for trusted headers.

mixed get(string $key, mixed $default = null, type $deep = false)

Gets a "parameter" value.

Session|null getSession()

Gets the Session.

boolean hasPreviousSession()

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

boolean hasSession()

Whether the request contains a Session object.

setSession(Session $session)

Sets the Session.

string getClientIp(Boolean $proxy = false)

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.

string getPort()

Returns the port on which the request is made.

string getHttpHost()

Returns the HTTP host being requested.

string getRequestUri()

Returns the requested URI.

string getUri()

Generates a normalized URI for the Request.

string getUriForPath(string $path)

Generates a normalized URI for the given path.

string|null getQueryString()

Generates the normalized query string for the Request.

Boolean 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 method.

string getMimeType(string $format)

Gets the mime type associated with the format.

string 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 $default = 'html')

Gets the request format.

setRequestFormat(string $format)

Sets the request format.

setLocale($locale)

getLocale()

Boolean isMethodSafe()

Checks whether the method is safe or not.

string|resource getContent(Boolean $asResource = false)

Returns the request body content.

array getETags()

Gets the Etags.

isNoCache()

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 getAcceptableContentTypes()

Gets a list of content types acceptable by the client browser

Boolean isXmlHttpRequest()

Returns true if the request is a XMLHttpRequest.

array splitHttpAcceptHeader(string $header)

Splits an Accept-* HTTP header.

Details

at line 132
public __construct(array $query = array(), array $request = array(), array $attributes = array(), array $cookies = array(), array $files = array(), array $server = array(), string $content = null)

Constructor.

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 $content The raw body data

at line 152
public initialize(array $query = array(), array $request = array(), array $attributes = array(), array $cookies = array(), array $files = array(), array $server = array(), string $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 $content The raw body data

at line 181
static public Request createFromGlobals()

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

Return Value

Request A new request

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

Creates a Request based on a given URI and configuration.

Parameters

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

Return Value

Request A Request instance

at line 294
public 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

at line 335
public __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 351
public string __toString()

Returns the request as a string.

Return Value

string The request

at line 366
public overrideGlobals()

Overrides the PHP global variables according to this request instance.

It overrides $GET, $POST, $REQUEST, $SERVER, $COOKIE, and $FILES.

at line 393
static public trustProxyData()

Trusts $_SERVER entries coming from proxies.

at line 407
static public setTrustedProxies(array $proxies)

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

at line 420
static public 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 434
static public array getTrustedHosts()

Gets the list of trusted host patterns.

Return Value

array An array of trusted host patterns.

at line 454
static public setTrustedHeaderName(string $key, string $value)

Sets the name for trusted headers.

The following header keys are supported:

Setting an empty value allows to disable the trusted header for the given key.

Parameters

string $key The header key
string $value The header name

at line 479
public mixed get(string $key, mixed $default = null, type $deep = false)

Gets a "parameter" value.

This method is mainly useful for libraries that want to provide some flexibility.

Order of precedence: GET, PATH, POST, COOKIE Avoid using this method in controllers: * slow * prefer to get from a "named" source

Parameters

string $key the key
mixed $default the default value
type $deep is parameter deep in multidimensional array

Return Value

mixed

at line 491
public Session|null getSession()

Gets the Session.

Return Value

Session|null The session

at line 504
public boolean hasPreviousSession()

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

Return Value

boolean

at line 517
public boolean hasSession()

Whether the request contains a Session object.

Return Value

boolean

at line 529
public setSession(Session $session)

Sets the Session.

Parameters

Session $session The Session

at line 557
public string getClientIp(Boolean $proxy = false)

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.

If your reverse proxy uses a different header name than "X-Forwarded-For", ("Client-Ip" for instance), configure it via "setTrustedHeaderName()" with the "client-ip" key.

Parameters

Boolean $proxy Whether the current request has been made behind a proxy or not (deprecated)

Return Value

string The client IP address

See also

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

at line 585
public string getScriptName()

Returns current script name.

Return Value

string

at line 605
public 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:

Return Value

string

at line 627
public string getBasePath()

Returns the root path from which this request is executed.

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

Return Value

string

at line 648
public 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

at line 664
public string getScheme()

Gets the request's scheme.

Return Value

string

at line 684
public 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.

If your reverse proxy uses a different header name than "X-Forwarded-Port", configure it via "setTrustedHeaderName()" with the "client-port" key.

Return Value

string

at line 702
public 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 721
public string getRequestUri()

Returns the requested URI.

Return Value

string

at line 739
public string getUri()

Generates a normalized URI for the Request.

Return Value

string A normalized URI for the Request

See also

getQueryString()

at line 758
public 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 773
public 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 813
public Boolean isSecure()

Checks whether the request is secure or not.

This method can read the client port 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".

If your reverse proxy uses a different header name than "X-Forwarded-Proto" ("SSL_HTTPS" for instance), configure it via "setTrustedHeaderName()" with the "client-proto" key.

Return Value

Boolean

at line 839
public string getHost()

Returns the host name.

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

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

If your reverse proxy uses a different header name than "X-Forwarded-Host", configure it via "setTrustedHeaderName()" with the "client-host" key.

Return Value

string

Exceptions

UnexpectedValueException when the host name is invalid

at line 888
public setMethod(string $method)

Sets the request method.

Parameters

string $method

at line 903
public string getMethod()

Gets the request method.

The method is always an uppercased string.

Return Value

string The request method

at line 924
public string getMimeType(string $format)

Gets the mime type associated with the format.

Parameters

string $format The format

Return Value

string The associated mime type (null if not found)

at line 942
public string getFormat(string $mimeType)

Gets the format associated with the mime type.

Parameters

string $mimeType The associated mime type

Return Value

string The format (null if not found)

at line 969
public 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 993
public string getRequestFormat(string $default = 'html')

Gets the request format.

Here is the process to determine the format:

Parameters

string $default The default format

Return Value

string The request format

at line 1009
public setRequestFormat(string $format)

Sets the request format.

Parameters

string $format The request format.

at line 1014
public setLocale($locale)

Parameters

$locale

at line 1023
public getLocale()

at line 1039
public Boolean isMethodSafe()

Checks whether the method is safe or not.

Return Value

Boolean

at line 1051
public string|resource getContent(Boolean $asResource = false)

Returns the request body content.

Parameters

Boolean $asResource If true, a resource will be returned

Return Value

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

at line 1075
public array getETags()

Gets the Etags.

Return Value

array The entity tags

at line 1080
public isNoCache()

at line 1094
public 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 1118
public array getLanguages()

Gets a list of languages acceptable by the client browser.

Return Value

array Languages ordered in the user browser preferences

at line 1160
public array getCharsets()

Gets a list of charsets acceptable by the client browser.

Return Value

array List of charsets in preferable order

at line 1176
public 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 1196
public Boolean isXmlHttpRequest()

Returns true if the request is a XMLHttpRequest.

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

Return Value

Boolean true if the request is an XMLHttpRequest, false otherwise

at line 1208
public array splitHttpAcceptHeader(string $header)

Splits an Accept-* HTTP header.

Parameters

string $header Header to split

Return Value

array Array indexed by the values of the Accept-* header in preferred order