class ApacheRequest extends Request
Request represents an HTTP request from an Apache server.
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. |
from Request | |
|
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. |
from Request | |
| static Request |
createFromGlobals()
Creates a new request with values from PHP's super globals. |
from Request |
| 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. |
from Request |
| 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. |
from Request |
|
__clone()
Clones the current request. |
from Request | |
| string |
__toString()
Returns the request as a string. |
from Request |
|
overrideGlobals()
Overrides the PHP global variables according to this request instance. |
from Request | |
| static |
setTrustedProxies(array $proxies)
Sets a list of trusted proxies. |
from Request |
| static array |
getTrustedProxies()
Gets the list of trusted proxies. |
from Request |
| static |
setTrustedHeaderName(string $key, string $value)
Sets the name for trusted headers. |
from Request |
| static string |
getTrustedHeaderName(string $key)
Gets the trusted proxy header name. |
from Request |
| static string |
normalizeQueryString(string $qs)
Normalizes a query string. |
from Request |
| static |
enableHttpMethodParameterOverride()
Enables support for the _method request parameter to determine the intended HTTP method. |
from Request |
| static Boolean |
getHttpMethodParameterOverride()
Checks whether support for the _method request parameter is enabled. |
from Request |
| mixed |
get(string $key, mixed $default = null, Boolean $deep = false)
Gets a "parameter" value. |
from Request |
| SessionInterface|null |
getSession()
Gets the Session. |
from Request |
| Boolean |
hasPreviousSession()
Whether the request contains a Session which was started in one of the previous requests. |
from Request |
| Boolean |
hasSession()
Whether the request contains a Session object. |
from Request |
|
setSession(SessionInterface $session)
Sets the Session. |
from Request | |
| array |
getClientIps()
Returns the client IP addresses. |
from Request |
| string |
getClientIp()
Returns the client IP address. |
from Request |
| string |
getScriptName()
Returns current script name. |
from Request |
| string |
getPathInfo()
Returns the path being requested relative to the executed script. |
from Request |
| string |
getBasePath()
Returns the root path from which this request is executed. |
from Request |
| string |
getBaseUrl()
Returns the root url from which this request is executed. |
from Request |
| string |
getScheme()
Gets the request's scheme. |
from Request |
| string |
getPort()
Returns the port on which the request is made. |
from Request |
| string|null |
getUser()
Returns the user. |
from Request |
| string|null |
getPassword()
Returns the password. |
from Request |
| string |
getUserInfo()
Gets the user info. |
from Request |
| string |
getHttpHost()
Returns the HTTP host being requested. |
from Request |
| string |
getRequestUri()
Returns the requested URI. |
from Request |
| string |
getSchemeAndHttpHost()
Gets the scheme and HTTP host. |
from Request |
| string |
getUri()
Generates a normalized URI for the Request. |
from Request |
| string |
getUriForPath(string $path)
Generates a normalized URI for the given path. |
from Request |
| string|null |
getQueryString()
Generates the normalized query string for the Request. |
from Request |
| Boolean |
isSecure()
Checks whether the request is secure or not. |
from Request |
| string |
getHost()
Returns the host name. |
from Request |
|
setMethod(string $method)
Sets the request method. |
from Request | |
| string |
getMethod()
Gets the request "intended" method. |
from Request |
| string |
getRealMethod()
Gets the "real" request method. |
from Request |
| string |
getMimeType(string $format)
Gets the mime type associated with the format. |
from Request |
| string|null |
getFormat(string $mimeType)
Gets the format associated with the mime type. |
from Request |
|
setFormat(string $format, string|array $mimeTypes)
Associates a format with mime types. |
from Request | |
| string |
getRequestFormat(string $default = 'html')
Gets the request format. |
from Request |
|
setRequestFormat(string $format)
Sets the request format. |
from Request | |
| string|null |
getContentType()
Gets the format associated with the request. |
from Request |
|
setDefaultLocale(string $locale)
Sets the default locale. |
from Request | |
|
setLocale(string $locale)
Sets the locale. |
from Request | |
| string |
getLocale()
Get the locale. |
from Request |
| Boolean |
isMethod(string $method)
Checks if the request method is of specified type. |
from Request |
| Boolean |
isMethodSafe()
Checks whether the method is safe or not. |
from Request |
| string|resource |
getContent(Boolean $asResource = false)
Returns the request body content. |
from Request |
| array |
getETags()
Gets the Etags. |
from Request |
| Boolean |
isNoCache()
|
from Request |
| string|null |
getPreferredLanguage(array $locales = null)
Returns the preferred language. |
from Request |
| array |
getLanguages()
Gets a list of languages acceptable by the client browser. |
from Request |
| array |
getCharsets()
Gets a list of charsets acceptable by the client browser. |
from Request |
| array |
getAcceptableContentTypes()
Gets a list of content types acceptable by the client browser |
from Request |
| Boolean |
isXmlHttpRequest()
Returns true if the request is a XMLHttpRequest. |
from Request |
Details
in Request at line 188
public
__construct(array $query = array(), array $request = array(), array $attributes = array(), array $cookies = array(), array $files = array(), array $server = array(), string $content = null)
Constructor.
in Request at line 208
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.
in Request at line 237
static public Request
createFromGlobals()
Creates a new request with values from PHP's super globals.
in Request at line 269
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.
The information contained in the URI always take precedence over the other information (server and parameters).
in Request at line 365
public 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.
in Request at line 406
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.
in Request at line 422
public string
__toString()
Returns the request as a string.
in Request at line 438
public
overrideGlobals()
Overrides the PHP global variables according to this request instance.
It overrides $GET, $POST, $REQUEST, $SERVER, $COOKIE. $FILES is never override, see rfc1867
in Request at line 474
static public
setTrustedProxies(array $proxies)
Sets a list of trusted proxies.
You should only list the reverse proxies that you manage directly.
in Request at line 484
static public array
getTrustedProxies()
Gets the list of trusted proxies.
in Request at line 506
static public
setTrustedHeaderName(string $key, string $value)
Sets the name for trusted headers.
The following header keys are supported:
- Request::HEADERCLIENTIP: defaults to X-Forwarded-For (see getClientIp())
- Request::HEADERCLIENTHOST: defaults to X-Forwarded-Host (see getClientHost())
- Request::HEADERCLIENTPORT: defaults to X-Forwarded-Port (see getClientPort())
- Request::HEADERCLIENTPROTO: defaults to X-Forwarded-Proto (see getScheme() and isSecure())
Setting an empty value allows to disable the trusted header for the given key.
in Request at line 524
static public string
getTrustedHeaderName(string $key)
Gets the trusted proxy header name.
in Request at line 543
static public 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.
in Request at line 584
static public
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.
The HTTP method can only be overridden when the real HTTP method is POST.
in Request at line 594
static public Boolean
getHttpMethodParameterOverride()
Checks whether support for the _method request parameter is enabled.
in Request at line 620
public mixed
get(string $key, mixed $default = null, Boolean $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
Avoid using this method in controllers:
- slow
- prefer to get from a "named" source
It is better to explicitly get request parameters from the appropriate public property instead (query, attributes, request).
in Request at line 632
public SessionInterface|null
getSession()
Gets the Session.
in Request at line 645
public Boolean
hasPreviousSession()
Whether the request contains a Session which was started in one of the previous requests.
in Request at line 662
public Boolean
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.
in Request at line 674
public
setSession(SessionInterface $session)
Sets the Session.
in Request at line 692
public array
getClientIps()
Returns the client IP addresses.
The most trusted IP address is first, and the less trusted one last. The "real" client IP address is the last one, but this is also the less trusted one.
Use this method carefully; you should use getClientIp() instead.
in Request at line 741
public string
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.
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.
in Request at line 755
public string
getScriptName()
Returns current script name.
in Request at line 776
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:
- 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'
in Request at line 799
public 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'
in Request at line 820
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.
in Request at line 836
public string
getScheme()
Gets the request's scheme.
in Request at line 856
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.
in Request at line 876
public string|null
getUser()
Returns the user.
in Request at line 886
public string|null
getPassword()
Returns the password.
in Request at line 896
public string
getUserInfo()
Gets the user info.
in Request at line 917
public string
getHttpHost()
Returns the HTTP host being requested.
The port name will be appended to the host if it's non-standard.
in Request at line 936
public string
getRequestUri()
Returns the requested URI.
in Request at line 953
public 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.
in Request at line 967
public string
getUri()
Generates a normalized URI for the Request.
in Request at line 985
public string
getUriForPath(string $path)
Generates a normalized URI for the given path.
in Request at line 1000
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.
in Request at line 1023
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.
in Request at line 1049
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.
in Request at line 1081
public
setMethod(string $method)
Sets the request method.
in Request at line 1104
public 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.
in Request at line 1128
public string
getRealMethod()
Gets the "real" request method.
in Request at line 1142
public string
getMimeType(string $format)
Gets the mime type associated with the format.
in Request at line 1160
public string|null
getFormat(string $mimeType)
Gets the format associated with the mime type.
in Request at line 1187
public
setFormat(string $format, string|array $mimeTypes)
Associates a format with mime types.
in Request at line 1211
public string
getRequestFormat(string $default = 'html')
Gets the request format.
Here is the process to determine the format:
- format defined by the user (with setRequestFormat())
- _format request parameter
- $default
in Request at line 1227
public
setRequestFormat(string $format)
Sets the request format.
in Request at line 1239
public string|null
getContentType()
Gets the format associated with the request.
in Request at line 1251
public
setDefaultLocale(string $locale)
Sets the default locale.
in Request at line 1267
public
setLocale(string $locale)
Sets the locale.
in Request at line 1277
public string
getLocale()
Get the locale.
in Request at line 1289
public Boolean
isMethod(string $method)
Checks if the request method is of specified type.
in Request at line 1301
public Boolean
isMethodSafe()
Checks whether the method is safe or not.
in Request at line 1315
public string|resource
getContent(Boolean $asResource = false)
Returns the request body content.
in Request at line 1339
public array
getETags()
Gets the Etags.
in Request at line 1347
public Boolean
isNoCache()
in Request at line 1361
public string|null
getPreferredLanguage(array $locales = null)
Returns the preferred language.
in Request at line 1396
public array
getLanguages()
Gets a list of languages acceptable by the client browser.
in Request at line 1438
public array
getCharsets()
Gets a list of charsets acceptable by the client browser.
in Request at line 1454
public array
getAcceptableContentTypes()
Gets a list of content types acceptable by the client browser
in Request at line 1474
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: