PHP Class lithium\action\Request

See also: lithium\action\Dispatcher
See also: lithium\action\Controller
See also: lithium\net\http\Router
See also: lithium\net\http\Route
See also: lithium\action\Request::__get()
Inheritance: extends lithium\net\http\Request
Show file Open project: unionofrad/lithium Class Usage Examples

Public Properties

Property Type Description
$data array Data found in the HTTP request body, most often populated by $_POST and $_FILES.
$params array Params for request.
$persist array Route parameters that should persist when generating URLs in this request context.
$query array Key/value pairs found encoded in the request URL after '?', populated by $_GET.
$url string Current url of request.

Protected Properties

Property Type Description
$_accept array Contains an array of content-types, sorted by quality (the priority which the browser requests each type).
$_autoConfig array Auto configuration properties.
$_base string Base path.
$_computed array Computed environment variables for the request. Retrieved with env().
$_detectors array embed:lithium\tests\cases\action\RequestTest::testRequestTypeIsMobile(4-4) Custom detectors can be added using detect().
$_env array Holds the server globals & environment variables.
$_locale string Holds the value of the current locale, set through the locale() method.
$_stream resource If POST, PUT or PATCH data is coming from an input stream (rather than $_POST), this specified where to read it from.

Public Methods

Method Description
__construct ( array $config = [] ) : void Constructor. Adds config values to the public properties when a new object is created, pulling request data from superglobals if globals is set to true.
__get ( string $name ) : mixed Allows request parameters to be accessed as object properties, i.e. $this->request->action instead of $this->request->params['action'].
__isset ( string $name ) : boolean Allows request parameters to be checked using short-hand notation. See the __get() method for more details.
accepts ( boolean | string $type = null ) : string | boolean | array Returns information about the type of content that the client is requesting.
detect ( string $flag, mixed $detector = null ) : void Creates a _detector_ used with Request::is(). A detector is a boolean check that is created to determine something about a request.
env ( string $key ) : string Queries PHP's environment settings, and provides an abstraction for standardizing expected environment values across varying platforms, as well as specify custom environment flags.
get ( string $key ) : string This method allows easy extraction of any request data using a prefixed key syntax. By passing keys in the form of 'prefix:key', it is possible to query different information of various different types, including GET and POST data, and server environment variables. The full list of prefixes is as follows:
is ( string $flag ) : boolean Provides a simple syntax for making assertions about the properties of a request.
locale ( string $locale = null ) : string Sets or returns the current locale string. For more information, see "Globalization" in the manual.
referer ( string $default = null, boolean $local = false ) : string Gets the referring URL of this request.
to ( string $format, array $options = [] ) : mixed Overrides lithium\net\http\Request::to() to provide the correct options for generating URLs. For information about this method, see the parent implementation.
type ( string $type = null ) : string Sets/Gets the content type. If 'type' is null, the method will attempt to determine the type from the params, then from the environment setting

Protected Methods

Method Description
_base ( string $base = null ) : string Find the base path of the current request.
_init ( ) Initializes request object by setting up mobile detectors, determining method and populating the data property either by using i.e. form data or reading from STDIN in case binary data is streamed. Will merge any files posted in forms with parsed data.
_parseAccept ( ) : array Parses the HTTP_ACCEPT information the requesting client sends, and converts that data to an array for consumption by the rest of the framework.
_parseFiles ( array $data ) : array Normalizes the data from the $_FILES superglobal.
_url ( $url = null ) : string Extract the url from REQUEST_URI && PHP_SELF environment variables.

Method Details

__construct() public method

Normalizes casing of request headers.
See also: lithium\net\http\Request::__construct()
See also: lithium\net\http\Message::__construct()
See also: lithium\net\Message::__construct()
public __construct ( array $config = [] ) : void
$config array The available configuration options are the following. Further options are inherited from the parent classes. - `'base'` _string_: Defaults to `null`. - `'url'` _string_: Defaults to `null`. - `'data'` _array_: Additional data to use when initializing the request. Defaults to `array()`. - `'stream'` _resource_: Stream to read from in order to get the message body when method is POST, PUT or PATCH and data is empty. When not provided `php://input` will be used for reading. - `'env'` _array_: Defaults to `array()`. - `'globals'` _boolean_: Use global variables for populating the request's environment and data; defaults to `true`.
return void

__get() public method

Allows request parameters to be accessed as object properties, i.e. $this->request->action instead of $this->request->params['action'].
See also: lithium\action\Request::$params
public __get ( string $name ) : mixed
$name string The property name/parameter key to return.
return mixed Returns the value of `$params[$name]` if it is set, otherwise returns null.

__isset() public method

Allows request parameters to be checked using short-hand notation. See the __get() method for more details.
See also: lithium\action\Request::__get()
public __isset ( string $name ) : boolean
$name string The name of the request parameter to check.
return boolean Returns true if the key in `$name` is set in the `$params` array, otherwise `false`.

_base() protected method

Find the base path of the current request.
protected _base ( string $base = null ) : string
$base string The base path. If `null`, `'PHP_SELF'` will be used instead.
return string

_init() protected method

Note that only beginning with PHP 5.6 STDIN can be opened/read and closed more than once.
See also: lithium\action\Request::_parseFiles()
protected _init ( )

_parseAccept() protected method

Parses the HTTP_ACCEPT information the requesting client sends, and converts that data to an array for consumption by the rest of the framework.
protected _parseAccept ( ) : array
return array All the types of content the client can accept.

_parseFiles() protected method

Normalizes the data from the $_FILES superglobal.
protected _parseFiles ( array $data ) : array
$data array Data as formatted in the `$_FILES` superglobal.
return array Normalized data.

_url() protected method

Extract the url from REQUEST_URI && PHP_SELF environment variables.
protected _url ( $url = null ) : string
return string

accepts() public method

This method may work different then you might think. This is a _convenience_ method working exclusively with short type names it knows about. Only those types will be matched. You can tell this method about more types via Media::type(). Note: In case negotiation fails, 'html' is used as a fallback type.
See also: lithium\net\http\Media::negotiate()
public accepts ( boolean | string $type = null ) : string | boolean | array
$type boolean | string Optionally a type name i.e. `'json'` or `true`. 1. If not specified, returns the media type name that the client prefers, using a potentially set `type` param, then content negotiation and that fails, ultimately falling back and returning the string `'html'`. 2. If a media type name (string) is passed, returns `true` or `false`, indicating whether or not that type is accepted by the client at all. 3. If `true`, returns the raw content types from the `Accept` header, parsed into an array and sorted by client preference.
return string | boolean | array Returns a type name (i.e. 'json'`) or a boolean or an array, depending on the value of `$type`.

detect() public method

A detector check can be either an exact string match or a regular expression match against a header or environment variable. A detector check can also be a closure that accepts the Request object instance as a parameter. For example, to detect whether a request is from an iPhone, you can do the following: embed:lithium\tests\cases\action\RequestTest::testDetect(11-12)
See also: lithium\action\Request::is()
public detect ( string $flag, mixed $detector = null ) : void
$flag string The name of the detector check. Used in subsequent calls to `Request::is()`.
$detector mixed Detectors can be specified in four different ways: - The name of an HTTP header or environment variable. If a string, calling the detector will check that the header or environment variable exists and is set to a non-empty value. - A two-element array containing a header/environment variable name, and a value to match against. The second element of the array must be an exact match to the header or variable value. - A two-element array containing a header/environment variable name, and a regular expression that matches against the value, as in the example above. - A closure which accepts an instance of the `Request` object and returns a boolean value.
return void

env() public method

Defines an artificial 'PLATFORM' environment variable as either 'IIS', 'CGI' or null to allow checking for the SAPI in a normalized way.
public env ( string $key ) : string
$key string The environment variable required.
return string The requested variables value.

get() public method

- 'data': Retrieves values from POST data. - 'params': Retrieves query parameters returned from the routing system. - 'query': Retrieves values from GET data. - 'env': Retrieves values from the server or environment, such as 'env:https', or custom environment values, like 'env:base'. See the env() method for more info. - 'http': Retrieves header values (i.e. 'http:accept'), or the HTTP request method (i.e. 'http:method'). This method is used in several different places in the framework in order to provide the ability to act conditionally on different aspects of the request. See Media::type() (the section on content negotiation) and the routing system for more information. _Note_: All keys should be _lower-cased_, even when getting HTTP headers.
See also: lithium\action\Request::env()
See also: lithium\net\http\Media::type()
See also: lithium\net\http\Router
public get ( string $key ) : string
$key string A prefixed key indicating what part of the request data the requested value should come from, and the name of the value to retrieve, in lower case.
return string Returns the value of a GET, POST, routing or environment variable, or an HTTP header or method name.

is() public method

By default, the Request object is configured with several different types of assertions, which are individually known as _detectors_. Detectors are invoked by calling the is() and passing the name of the detector flag, i.e. $request->is(''), which returns true or false, depending on whether or not the the properties (usually headers or data) contained in the request match the detector. The default detectors include the following: - 'mobile': Uses a regular expression to match common mobile browser user agents. - 'ajax': Checks to see if the X-Requested-With header is present, and matches the value 'XMLHttpRequest'. - 'flash': Checks to see if the user agent is 'Shockwave Flash'. - 'ssl': Verifies that the request is SSL-secured. - 'get' / 'post' / 'put' / 'delete' / 'head' / 'options': Checks that the HTTP request method matches the one specified. In addition to the above, this method also accepts media type names (see Media::type()) to make assertions against the format of the request body (for POST or PUT requests), i.e. $request->is('json'). This will return true if the client has made a POST request with JSON data. For information about adding custom detectors or overriding the ones in the core, see the detect() method. While these detectors are useful in controllers or other similar contexts, they're also useful when performing _content negotiation_, which is the process of modifying the response format to suit the client (see the 'conditions' field of the $options parameter in Media::type()).
See also: lithium\action\Request::detect()
See also: lithium\net\http\Media::type()
public is ( string $flag ) : boolean
$flag string The name of the flag to check, which should be the name of a valid detector (that is either built-in or defined with `detect()`).
return boolean Returns `true` if the detector check succeeds (see the details for the built-in detectors above, or `detect()`), otherwise `false`.

locale() public method

Sets or returns the current locale string. For more information, see "Globalization" in the manual.
public locale ( string $locale = null ) : string
$locale string An optional locale string like `'en'`, `'en_US'` or `'de_DE'`. If specified, will overwrite the existing locale.
return string Returns the currently set locale string.

referer() public method

Gets the referring URL of this request.
public referer ( string $default = null, boolean $local = false ) : string
$default string Default URL to use if HTTP_REFERER cannot be read from headers.
$local boolean If true, restrict referring URLs to local server.
return string Referring URL.

to() public method

Overrides lithium\net\http\Request::to() to provide the correct options for generating URLs. For information about this method, see the parent implementation.
See also: lithium\net\http\Request::to()
public to ( string $format, array $options = [] ) : mixed
$format string The format to convert to.
$options array Override options.
return mixed The return value type depends on `$format`.

type() public method

Sets/Gets the content type. If 'type' is null, the method will attempt to determine the type from the params, then from the environment setting
public type ( string $type = null ) : string
$type string a full content type i.e. `'application/json'` or simple name `'json'`
return string A simple content type name, i.e. `'html'`, `'xml'`, `'json'`, etc., depending on the content type of the request.

Property Details

$_accept protected property

Contains an array of content-types, sorted by quality (the priority which the browser requests each type).
protected array $_accept
return array

$_autoConfig protected property

Auto configuration properties.
protected array $_autoConfig
return array

$_base protected property

Base path.
protected string $_base
return string

$_computed protected property

Computed environment variables for the request. Retrieved with env().
See also: lithium\action\Request::env()
protected array $_computed
return array

$_detectors protected property

embed:lithium\tests\cases\action\RequestTest::testRequestTypeIsMobile(4-4) Custom detectors can be added using detect().
See also: lithium\action\Request::is()
See also: lithium\action\Request::detect()
protected array $_detectors
return array

$_env protected property

Holds the server globals & environment variables.
protected array $_env
return array

$_locale protected property

Holds the value of the current locale, set through the locale() method.
protected string $_locale
return string

$_stream protected property

If POST, PUT or PATCH data is coming from an input stream (rather than $_POST), this specified where to read it from.
See also: lithium\action\Request::_init()
protected resource $_stream
return resource

$data public property

Data found in the HTTP request body, most often populated by $_POST and $_FILES.
public array $data
return array

$params public property

Params for request.
public array $params
return array

$persist public property

Route parameters that should persist when generating URLs in this request context.
public array $persist
return array

$query public property

Key/value pairs found encoded in the request URL after '?', populated by $_GET.
public array $query
return array

$url public property

Current url of request.
public string $url
return string