A class that helps wrap Request information and particulars about a single request. 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
.
$_detectors
protected array
is()
can be modified with addDetector()
. $_input
protected 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.
$base
public string
$data
public array
Array of POST data. Will contain form data as well as uploaded files. 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
public string
$params
public array
$query
public array
$url
public string
$webroot
public string
Magic isset method allows isset/empty checks on routing parameters.
Recursively walks the FILES array restructuring the data into something sane and useable.
process the post data and set what is there into the object. processed data is available at $this->data
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. Each of these server variables have the base path, and query strings stripped off
Find out which content types the client accepts or check if they accept a particular type of content.
Add a new detector to the list of detectors that a request can use. There are several different formats and types of detectors that can be set.
Add parameters to the request's parsed parameter set. This will overwrite any existing parameters. This modifies the parameters available through $request->params
.
Add paths to the requests' paths vars. This will overwrite any existing paths. Provides an easy way to modify, here, webroot and base.
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.
Provides a read/write accessor for $this->data
. Allows you to use a syntax similar to CakeSession
for reading post data.
Read data from php://input
. Useful when interacting with XML or JSON request body content.
Get the HTTP method used for this request. There are a few ways to specify a method.
Parse the HTTP_ACCEPT header and return a sorted array with content types as the keys, and pref values as the values.
Provides a read accessor for $this->query
. Allows you to use a syntax similar to CakeSession
for reading URL query data.
Modify data originally from php://input
. Useful for altering json/xml data in middleware or DispatcherFilters before it gets to RequestHandlerComponent
__call( string $name , array $params )
Missing method handler, handles wrapping older style isAjax() type methods
$name
$params
CakeException
__construct( string $url null , boolean $parseEnvironment true )
Constructor
$url
optional null $parseEnvironment
optional true __get( string $name )
Magic get method allows access to parsed routing parameters directly on the object.
Allows access to $this->params['controller']
via $this->controller
$name
__isset( string $name )
Magic isset method allows isset/empty checks on routing parameters.
$name
_acceptHeaderDetector( array $detect )
Detects if a specific accept header is present.
$detect
_base( )
Returns a base URL and sets the proper webroot
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.
_environmentDetector( array $detect )
Detects if a specific environment variable is present.
$detect
_extensionDetector( array $detect )
Detects if a URL extension is present.
$detect
_headerDetector( array $detect )
Detects if a specific header is present.
$detect
_paramDetector( array $detect )
Detects if a specific request parameter is present.
$detect
_parseAcceptWithQualifier( string $header )
Parse Accept* headers with qualifier options.
Only qualifiers will be extracted, any other accept extensions will be discarded as they are not frequently used.
$header
_processFileData( string $path , array $data , string $field )
Recursively walks the FILES array restructuring the data into something sane and useable.
$path
$data
$field
_processGet( )
Process the GET parameters and move things into the object.
_processPost( )
process the post data and set what is there into the object. 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.
_readInput( )
Read data from php://input, mocked in tests.
_url( )
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. Each of these server variables have the base path, and query strings stripped off
acceptLanguage( string $language null )
Get the languages accepted by the client, or check if a specific language is accepted.
Get the list of accepted languages:
CakeRequest::acceptLanguage(); ``` Check if a specific language is accepted:
CakeRequest::acceptLanguage('es-es'); ```
$language
optional null accepts( string $type null )
Find out which content types the client accepts or check if they accept a particular type of content.
$this->request->accepts();
$this->request->accepts('application/json');
This method will order the returned content types by the preference values indicated by the client.
$type
optional null Either an array of all the types the client accepts or a boolean if they accept the provided type.
addDetector( string $name , array $options )
Add a new detector to the list of detectors that a request can use. There are several different formats and types of detectors that can be set.
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 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 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 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')));
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'))
$name
$options
addParams( array $params )
Add parameters to the request's parsed parameter set. This will overwrite any existing parameters. This modifies the parameters available through $request->params
.
$params
CakeRequest
addPaths( array $paths )
Add paths to the requests' paths vars. This will overwrite any existing paths. Provides an easy way to modify, here, webroot and base.
$paths
CakeRequest
allowMethod( string|array $methods )
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.
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.
$methods
MethodNotAllowedException
clientIp( boolean $safe true )
Get the IP the client is using, or says they are using.
$safe
optional true 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
data( string $name )
Provides a read/write accessor for $this->data
. Allows you to use a syntax similar to CakeSession
for reading post data.
$request->data('Post.title');
When reading values you will get null
for keys/values that do not exist.
$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.
$name
CakeRequest
domain( integer $tldLength 1 )
Get the domain name and include $tldLength segments of the tld.
$tldLength
optional 1 Number of segments your tld contains. For example: example.com
contains 1 tld. While example.co.uk
contains 2.
header( string $name )
Read an HTTP header from the Request information.
$name
here( boolean $base true )
Get the value of the current requests URL. Will include named parameters and querystring arguments.
$base
optional true host( boolean $trustProxy false )
Get the host that the request was handled on.
$trustProxy
optional false input( string $callback null )
Read data from php://input
. Useful when interacting with XML or JSON request body content.
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.
$callback
optional null 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.
is( string|array $type )
Check whether or not a Request is a certain type.
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()
.
$type
The type of request you want to check. If an array this method will return true if the request matches any type.
isAll( array $types )
Check that a request matches all the given types.
Allows you to test multiple types and union the results. See CakeRequest::is() for how to add additional types and the built-in types.
$types
method( )
Get the HTTP method used for this request. There are a few ways to specify a method.
_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.
offsetExists( string $name )
Array access isset() implementation
$name
ArrayAccess::offsetExists()
offsetGet( string $name )
Array access read implementation
$name
ArrayAccess::offsetGet()
offsetSet( string $name , mixed $value )
Array access write implementation
$name
$value
ArrayAccess::offsetSet()
offsetUnset( string $name )
Array access unset() implementation
$name
ArrayAccess::offsetUnset()
onlyAllow( string|array $methods )
Alias of CakeRequest::allowMethod() for backwards compatibility.
$methods
MethodNotAllowedException
param( string $name )
Safely access the values in $this->params.
$name
The value of the provided parameter. Will return false if the parameter doesn't exist or is falsey.
parseAccept( )
Parse the HTTP_ACCEPT header and return a sorted array with content types as the keys, and pref values as the values.
Generally you want to use CakeRequest::accept() to get a simple list of the accepted content types.
query( string $name )
Provides a read accessor for $this->query
. Allows you to use a syntax similar to CakeSession
for reading URL query data.
$name
referer( boolean $local false )
Returns the referer that referred this request.
$local
optional false setInput( string $input )
Modify data originally from php://input
. Useful for altering json/xml data in middleware or DispatcherFilters before it gets to RequestHandlerComponent
$input
subdomains( integer $tldLength 1 )
Get the subdomains for a host.
$tldLength
optional 1 Number of segments your tld contains. For example: example.com
contains 1 tld. While example.co.uk
contains 2.
protected array
The built in detectors used with is()
can be modified with addDetector()
.
There are several ways to specify a detector, see CakeRequest::addDetector() for the various formats and ways to define detectors.
array( 'get' => array('env' => 'REQUEST_METHOD', 'value' => 'GET'), 'patch' => array('env' => 'REQUEST_METHOD', 'value' => 'PATCH'), 'post' => array('env' => 'REQUEST_METHOD', 'value' => 'POST'), 'put' => array('env' => 'REQUEST_METHOD', 'value' => 'PUT'), 'delete' => array('env' => 'REQUEST_METHOD', 'value' => 'DELETE'), 'head' => array('env' => 'REQUEST_METHOD', 'value' => 'HEAD'), 'options' => array('env' => 'REQUEST_METHOD', 'value' => 'OPTIONS'), 'ssl' => array('env' => 'HTTPS', 'value' => 1), 'ajax' => array('env' => 'HTTP_X_REQUESTED_WITH', 'value' => 'XMLHttpRequest'), 'flash' => array('env' => 'HTTP_USER_AGENT', 'pattern' => '/^(Shockwave|Adobe) Flash/'), 'mobile' => array('env' => 'HTTP_USER_AGENT', 'options' => array( 'Android', 'AvantGo', 'BB10', 'BlackBerry', 'DoCoMo', 'Fennec', 'iPod', 'iPhone', 'iPad', 'J2ME', 'MIDP', 'NetFront', 'Nokia', 'Opera Mini', 'Opera Mobi', 'PalmOS', 'PalmSource', 'portalmmm', 'Plucker', 'ReqwirelessWeb', 'SonyEricsson', 'Symbian', 'UP\\.Browser', 'webOS', 'Windows CE', 'Windows Phone OS', 'Xiino' )), 'requested' => array('param' => 'requested', 'value' => 1), 'json' => array('accept' => array('application/json'), 'param' => 'ext', 'value' => 'json'), 'xml' => array('accept' => array('application/xml', 'text/xml'), 'param' => 'ext', 'value' => 'xml'), )
protected 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 array
Array of POST data. Will contain form data as well as uploaded files. 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.
array()
public array
Array of parameters parsed from the URL.
array( 'plugin' => null, 'controller' => null, 'action' => null, 'named' => array(), 'pass' => array(), )
© 2005–2016 The Cake Software Foundation, Inc.
Licensed under the MIT License.
CakePHP is a registered trademark of Cake Software Foundation, Inc.
We are not endorsed by or affiliated with CakePHP.
https://api.cakephp.org/2.9/class-CakeRequest.html