PHP Class CakeRequest

Provides methods commonly used to introspect on the request headers and request body. Has both an Array and Object interface. You can access framework parameters using indexes: $request['controller'] or $request->controller.
Inheritance: implements ArrayAccess
Mostra file Open project: baserproject/basercms Class Usage Examples

Public Properties

Property Type Description
$base string Base URL path.
$data array Inputs prefixed with 'data' will have the data prefix removed. If there is overlap between an input prefixed with data and one without, the 'data' prefixed value will take precedence.
$here string The full address to the current request
$params array Array of parameters parsed from the URL.
$query array Array of querystring arguments
$url string The URL string used for the request.
$webroot string webroot path segment for the request.

Protected Properties

Property Type Description
$_detectors array There are several ways to specify a detector, see CakeRequest::addDetector() for the various formats and ways to define detectors.
$_input string Copy of php://input. Since this stream can only be read once in most SAPI's keep a copy of it so users don't need to know about that detail.

Public Methods

Method Description
__call ( string $name, array $params ) : mixed Missing method handler, handles wrapping older style isAjax() type methods
__construct ( string $url = null, boolean $parseEnvironment = true ) Constructor
__get ( string $name ) : mixed Magic get method allows access to parsed routing parameters directly on the object.
__isset ( string $name ) : boolean Magic isset method allows isset/empty checks on routing parameters.
acceptLanguage ( string $language = null ) : mixed Get the languages accepted by the client, or check if a specific language is accepted.
accepts ( string $type = null ) : mixed Find out which content types the client accepts or check if they accept a particular type of content.
addDetector ( string $name, array $options ) : void Add a new detector to the list of detectors that a request can use.
addParams ( array $params ) : self Add parameters to the request's parsed parameter set. This will overwrite any existing parameters.
addPaths ( array $paths ) : self Add paths to the requests' paths vars. This will overwrite any existing paths.
allowMethod ( string | array $methods ) : boolean 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.
clientIp ( boolean $safe = true ) : string Get the IP the client is using, or says they are using.
contentType ( ) : string Get the content type used in this request.
data ( string $name ) : mixed | self Provides a read/write accessor for $this->data. Allows you to use a syntax similar to CakeSession for reading post data.
domain ( integer $tldLength = 1 ) : string Get the domain name and include $tldLength segments of the tld.
header ( string $name ) : mixed Read an HTTP header from the Request information.
here ( boolean $base = true ) : string Get the value of the current requests URL. Will include named parameters and querystring arguments.
host ( boolean $trustProxy = false ) : string Get the host that the request was handled on.
input ( string $callback = null ) : The Read data from php://input. Useful when interacting with XML or JSON request body content.
is ( string | array $type ) : boolean Check whether or not a Request is a certain type.
isAll ( array $types ) : boolean Check that a request matches all the given types.
method ( ) : string Get the HTTP method used for this request.
normalizedHere ( ) : string 現在のURLを正規化して取得する
offsetExists ( string $name ) : boolean Array access isset() implementation
offsetGet ( string $name ) : mixed Array access read implementation
offsetSet ( string $name, mixed $value ) : void Array access write implementation
offsetUnset ( string $name ) : void Array access unset() implementation
onlyAllow ( string | array $methods ) : boolean Alias of CakeRequest::allowMethod() for backwards compatibility.
param ( string $name ) : mixed Safely access the values in $this->params.
parseAccept ( ) : array Parse the HTTP_ACCEPT header and return a sorted array with content types as the keys, and pref values as the values.
query ( string $name ) : mixed Provides a read accessor for $this->query. Allows you to use a syntax similar to CakeSession for reading URL query data.
referer ( boolean $local = false ) : string Returns the referer that referred this request.
setInput ( string $input ) : void Modify data originally from php://input. Useful for altering json/xml data in middleware or DispatcherFilters before it gets to RequestHandlerComponent
subdomains ( integer $tldLength = 1 ) : array Get the subdomains for a host.

Protected Methods

Method Description
_acceptHeaderDetector ( array $detect ) : boolean Detects if a specific accept header is present.
_base ( ) : string Returns a base URL and sets the proper webroot
_environmentDetector ( array $detect ) : boolean Detects if a specific environment variable is present.
_extensionDetector ( array $detect ) : boolean Detects if a URL extension is present.
_headerDetector ( array $detect ) : boolean Detects if a specific header is present.
_paramDetector ( array $detect ) : boolean Detects if a specific request parameter is present.
_parseAcceptWithQualifier ( string $header ) : array Parse Accept* headers with qualifier options.
_processFileData ( string $path, array $data, string $field ) : void Recursively walks the FILES array restructuring the data into something sane and useable.
_processFiles ( ) : void Process $_FILES and move things into the object.
_processGet ( ) : void Process the GET parameters and move things into the object.
_processPost ( ) : void process the post data and set what is there into the object.
_readInput ( ) : string Read data from php://input, mocked in tests.
_url ( ) : string Get the request uri. Looks in PATH_INFO first, as this is the exact value we need prepared by PHP. Following that, REQUEST_URI, PHP_SELF, HTTP_X_REWRITE_URL and argv are checked in that order.

Method Details

__call() public method

Missing method handler, handles wrapping older style isAjax() type methods
public __call ( string $name, array $params ) : mixed
$name string The method called
$params array Array of parameters for the method call
return mixed

__construct() public method

Constructor
public __construct ( string $url = null, boolean $parseEnvironment = true )
$url string Trimmed URL string to use. Should not contain the application base path.
$parseEnvironment boolean Set to false to not auto parse the environment. ie. GET, POST and FILES.

__get() public method

Allows access to $this->params['controller'] via $this->controller
public __get ( string $name ) : mixed
$name string The property being accessed.
return mixed Either the value of the parameter or null.

__isset() public method

Magic isset method allows isset/empty checks on routing parameters.
public __isset ( string $name ) : boolean
$name string The property being accessed.
return boolean Existence

_acceptHeaderDetector() protected method

Detects if a specific accept header is present.
protected _acceptHeaderDetector ( array $detect ) : boolean
$detect array Detector options array.
return boolean Whether or not the request is the type you are checking.

_base() protected method

If CakePHP is called with index.php in the URL even though URL Rewriting is activated (and thus not needed) it swallows the unnecessary part from $base to prevent issue #3318.
protected _base ( ) : string
return string Base URL

_environmentDetector() protected method

Detects if a specific environment variable is present.
protected _environmentDetector ( array $detect ) : boolean
$detect array Detector options array.
return boolean Whether or not the request is the type you are checking.

_extensionDetector() protected method

Detects if a URL extension is present.
protected _extensionDetector ( array $detect ) : boolean
$detect array Detector options array.
return boolean Whether or not the request is the type you are checking.

_headerDetector() protected method

Detects if a specific header is present.
protected _headerDetector ( array $detect ) : boolean
$detect array Detector options array.
return boolean Whether or not the request is the type you are checking.

_paramDetector() protected method

Detects if a specific request parameter is present.
protected _paramDetector ( array $detect ) : boolean
$detect array Detector options array.
return boolean Whether or not the request is the type you are checking.

_parseAcceptWithQualifier() protected static method

Only qualifiers will be extracted, any other accept extensions will be discarded as they are not frequently used.
protected static _parseAcceptWithQualifier ( string $header ) : array
$header string Header to parse.
return array

_processFileData() protected method

Recursively walks the FILES array restructuring the data into something sane and useable.
protected _processFileData ( string $path, array $data, string $field ) : void
$path string The dot separated path to insert $data into.
$data array The data to traverse/insert.
$field string The terminal field name, which is the top level key in $_FILES.
return void

_processFiles() protected method

Process $_FILES and move things into the object.
protected _processFiles ( ) : void
return void

_processGet() protected method

Process the GET parameters and move things into the object.
protected _processGet ( ) : void
return void

_processPost() protected method

processed data is available at $this->data Will merge POST vars prefixed with data, and ones without into a single array. Variables prefixed with data will overwrite those without. If you have mixed POST values be careful not to make any top level keys numeric containing arrays. Hash::merge() is used to merge data, and it has possibly unexpected behavior in this situation.
protected _processPost ( ) : void
return void

_readInput() protected method

Read data from php://input, mocked in tests.
protected _readInput ( ) : string
return string contents of php://input

_url() protected method

Each of these server variables have the base path, and query strings stripped off
protected _url ( ) : string
return string URI The CakePHP request path that is being accessed.

acceptLanguage() public static method

Get the list of accepted languages: CakeRequest::acceptLanguage(); Check if a specific language is accepted: CakeRequest::acceptLanguage('es-es');
public static acceptLanguage ( string $language = null ) : mixed
$language string The language to test.
return mixed If a $language is provided, a boolean. Otherwise the array of accepted languages.

accepts() public method

#### 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.
public accepts ( string $type = null ) : mixed
$type string The content type to check for. Leave null to get all types a client accepts.
return mixed Either an array of all the types the client accepts or a boolean if they accept the provided type.

addDetector() public method

There are several different formats and types of detectors that can be set. ### 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. e.g addDetector('post', array('env' => 'REQUEST_METHOD', 'value' => 'POST')) ### Pattern value comparison Pattern value comparison allows you to compare a value fetched from env() to a regular expression. e.g addDetector('iphone', array('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. e.g addDetector('mobile', array('env' => 'HTTP_USER_AGENT', 'options' => array('Fennec'))); ### Callback detectors Callback detectors allow you to provide a 'callback' type to handle the check. The callback will receive the request object as its only parameter. e.g addDetector('custom', array('callback' => array('SomeClass', 'somemethod'))); ### Request parameter detectors Allows for custom detectors on the request parameters. e.g addDetector('requested', array('param' => 'requested', 'value' => 1) You can also make parameter detectors that accept multiple values using the options key. This is useful when you want to check if a request parameter is in a list of options. addDetector('extension', array('param' => 'ext', 'options' => array('pdf', 'csv'))
public addDetector ( string $name, array $options ) : void
$name string The name of the detector.
$options array The options for the detector definition. See above.
return void

addParams() public method

This modifies the parameters available through $request->params.
public addParams ( array $params ) : self
$params array Array of parameters to merge in
return self

addPaths() public method

Provides an easy way to modify, here, webroot and base.
public addPaths ( array $paths ) : self
$paths array Array of paths to merge in
return self

allowMethod() public method

Example: $this->request->allowMethod('post', 'delete'); or $this->request->allowMethod(array('post', 'delete')); If the request would be GET, response header "Allow: POST, DELETE" will be set and a 405 error will be returned.
public allowMethod ( string | array $methods ) : boolean
$methods string | array Allowed HTTP request methods.
return boolean true

clientIp() public method

Get the IP the client is using, or says they are using.
public clientIp ( boolean $safe = true ) : string
$safe boolean Use safe = false when you think the user might manipulate their HTTP_CLIENT_IP header. Setting $safe = false will also look at HTTP_X_FORWARDED_FOR
return string The client IP.

contentType() public method

Get the content type used in this request.
public contentType ( ) : string
return string

data() public method

## Reading values. $request->data('Post.title'); When reading values you will get null for keys/values that do not exist. ## Writing values $request->data('Post.title', 'New post!'); You can write to any value, even paths/keys that do not exist, and the arrays will be created for you.
public data ( string $name ) : mixed | self
$name string Dot separated name of the value to read/write, one or more args.
return mixed | self Either the value being read, or $this so you can chain consecutive writes.

domain() public method

Get the domain name and include $tldLength segments of the tld.
public domain ( integer $tldLength = 1 ) : string
$tldLength integer Number of segments your tld contains. For example: `example.com` contains 1 tld. While `example.co.uk` contains 2.
return string Domain name without subdomains.

header() public static method

Read an HTTP header from the Request information.
public static header ( string $name ) : mixed
$name string Name of the header you want.
return mixed Either false on no header being set or the value of the header.

here() public method

Get the value of the current requests URL. Will include named parameters and querystring arguments.
public here ( boolean $base = true ) : string
$base boolean Include the base path, set to false to trim the base path off.
return string the current request URL including query string args.

host() public method

Get the host that the request was handled on.
public host ( boolean $trustProxy = false ) : string
$trustProxy boolean Whether or not to trust the proxy host.
return string

input() public method

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', array('return' => 'DOMDocument')); Any additional parameters are applied to the callback in the order they are given.
public input ( string $callback = null ) : The
$callback string 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.
return The decoded/processed request data.

is() public method

Uses the built in detection rules as well as additional rules defined with CakeRequest::addDetector(). Any detector can be called as is($type) or is$Type().
public is ( string | array $type ) : boolean
$type string | array The type of request you want to check. If an array this method will return true if the request matches any type.
return boolean Whether or not the request is the type you are checking.

isAll() public method

Allows you to test multiple types and union the results. See CakeRequest::is() for how to add additional types and the built-in types.
See also: CakeRequest::is()
public isAll ( array $types ) : boolean
$types array The types to check.
return boolean Success.

method() public method

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.
public method ( ) : string
return string The name of the HTTP method used.

normalizedHere() public method

$this->request->here は、ビューキャッシュの命名規則に影響する為、 CacheHelper 等で、このメソッドを利用する事で、同一ページによる複数キャッシュの生成を防ぐ (例) news/ → /news/index company/ → /company/index
public normalizedHere ( ) : string
return string

offsetExists() public method

Array access isset() implementation
public offsetExists ( string $name ) : boolean
$name string thing to check.
return boolean

offsetGet() public method

Array access read implementation
public offsetGet ( string $name ) : mixed
$name string Name of the key being accessed.
return mixed

offsetSet() public method

Array access write implementation
public offsetSet ( string $name, mixed $value ) : void
$name string Name of the key being written
$value mixed The value being written.
return void

offsetUnset() public method

Array access unset() implementation
public offsetUnset ( string $name ) : void
$name string Name to unset.
return void

onlyAllow() public method

Alias of CakeRequest::allowMethod() for backwards compatibility.
See also: CakeRequest::allowMethod()
Deprecation: 3.0.0 Since 2.5, use CakeRequest::allowMethod() instead.
public onlyAllow ( string | array $methods ) : boolean
$methods string | array Allowed HTTP request methods.
return boolean true

param() public method

Safely access the values in $this->params.
public param ( string $name ) : mixed
$name string The name of the parameter to get.
return mixed The value of the provided parameter. Will return false if the parameter doesn't exist or is falsey.

parseAccept() public method

Generally you want to use CakeRequest::accept() to get a simple list of the accepted content types.
public parseAccept ( ) : array
return array An array of prefValue => array(content/types)

query() public method

Provides a read accessor for $this->query. Allows you to use a syntax similar to CakeSession for reading URL query data.
public query ( string $name ) : mixed
$name string Query string variable name
return mixed The value being read

referer() public method

Returns the referer that referred this request.
public referer ( boolean $local = false ) : string
$local boolean Attempt to return a local address. Local addresses do not contain hostnames.
return string The referring address for this request.

setInput() public method

Modify data originally from php://input. Useful for altering json/xml data in middleware or DispatcherFilters before it gets to RequestHandlerComponent
public setInput ( string $input ) : void
$input string A string to replace original parsed data from input()
return void

subdomains() public method

Get the subdomains for a host.
public subdomains ( integer $tldLength = 1 ) : array
$tldLength integer Number of segments your tld contains. For example: `example.com` contains 1 tld. While `example.co.uk` contains 2.
return array An array of subdomains.

Property Details

$_detectors protected_oe property

There are several ways to specify a detector, see CakeRequest::addDetector() for the various formats and ways to define detectors.
protected array $_detectors
return array

$_input protected_oe property

Copy of php://input. Since this stream can only be read once in most SAPI's keep a copy of it so users don't need to know about that detail.
protected string $_input
return string

$base public_oe property

Base URL path.
public string $base
return string

$data public_oe property

Inputs prefixed with 'data' will have the data prefix removed. If there is overlap between an input prefixed with data and one without, the 'data' prefixed value will take precedence.
public array $data
return array

$here public_oe property

The full address to the current request
public string $here
return string

$params public_oe property

Array of parameters parsed from the URL.
public array $params
return array

$query public_oe property

Array of querystring arguments
public array $query
return array

$url public_oe property

The URL string used for the request.
public string $url
return string

$webroot public_oe property

webroot path segment for the request.
public string $webroot
return string