Class CakeRequest
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
.
- CakeRequest implements ArrayAccess
Copyright: Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
License: License (http://www.opensource.org/licenses/mit-license.php)
Location: Cake/Network/CakeRequest.php
Properties summary
-
$_detectors
protectedarray
The built in detectors used withis()
can be modified withaddDetector()
. -
$_input
protectedstring
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
publicstring
Base url path. -
$data
publicarray
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
publicstring
The full address to the current request -
$params
publicarray
Array of parameters parsed from the url. -
$query
publicarray
Array of querystring arguments -
$url
publicstring
The url string used for the request. -
$webroot
publicstring
webroot path segment for the request.
Method Summary
-
__call() public
Missing method handler, handles wrapping older style isAjax() type methods -
__construct() public
Constructor -
__get() public
Magic get method allows access to parsed routing parameters directly on the object. -
__isset() public
Magic isset method allows isset/empty checks on routing parameters.
-
_base() protected
Returns a base URL and sets the proper webroot -
_processFileData() protected
Recursively walks the FILES array restructuring the data into something sane and useable.
-
_processFiles() protected
Process $_FILES and move things into the object. -
_processGet() protected
Process the GET parameters and move things into the object. -
_processPost() protected
process the post data and set what is there into the object. processed data is available at
$this->data
-
_readInput() protected
Read data from php://input, mocked in tests. -
_url() protected
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() public static
Get the languages accepted by the client, or check if a specific language is accepted. -
accepts() public
Find out which content types the client accepts or check if they accept a particular type of content.
-
addDetector() public
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.
-
addParams() public
Add parameters to the request's parsed parameter set. This will overwrite any existing parameters. This modifies the parameters available through
$request->params
. -
addPaths() public
Add paths to the requests' paths vars. This will overwrite any existing paths. Provides an easy way to modify, here, webroot and base.
-
clientIp() public
Get the IP the client is using, or says they are using. -
data() public
Provides a read/write accessor for
$this->data
. Allows you to use a syntax similar toCakeSession
for reading post data. -
domain() public
Get the domain name and include $tldLength segments of the tld. -
header() public static
Read an HTTP header from the Request information. -
here() public
Get the value of the current requests url. Will include named parameters and querystring arguments. -
host() public
Get the host that the request was handled on. -
input() public
Read data from
php://input
. Useful when interacting with XML or JSON request body content. -
is() public
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)
oris$Type()
. -
method() public
Get the HTTP method used for this request. There are a few ways to specify a method.
-
offsetExists() public
Array access isset() implementation -
offsetGet() public
Array access read implementation -
offsetSet() public
Array access write implementation -
offsetUnset() public
Array access unset() implementation -
parseAccept() public
Parse the HTTP_ACCEPT header and return a sorted array with content types as the keys, and pref values as the values.
-
referer() public
Returns the referer that referred this request. -
subdomains() public
Get the subdomains for a host.
Method Detail
__call() public ¶
__call( string $name , array $params )
Missing method handler, handles wrapping older style isAjax() type methods
Parameters
- string $name
- The method called
- array $params
- Array of parameters for the method call
Returns
Throws
__construct() public ¶
__construct( string $url = null , boolean $parseEnvironment = true )
Constructor
Parameters
- string $url optional null
- Trimmed url string to use. Should not contain the application base path.
- boolean $parseEnvironment optional true
- Set to false to not auto parse the environment. ie. GET, POST and FILES.
__get() public ¶
__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
Parameters
- string $name
- The property being accessed.
Returns
Either the value of the parameter or null.
__isset() public ¶
__isset( string $name )
Magic isset method allows isset/empty checks on routing parameters.
Parameters
- string $name
- The property being accessed.
Returns
Existence
_processFileData() protected ¶
_processFileData( string $path , array $data , string $field )
Recursively walks the FILES array restructuring the data into something sane and useable.
Parameters
- string $path
- The dot separated path to insert $data into.
- array $data
- The data to traverse/insert.
- string $field
- The terminal field name, which is the top level key in $_FILES.
_processGet() protected ¶
_processGet( )
Process the GET parameters and move things into the object.
_processPost() protected ¶
_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() protected ¶
_readInput( )
Read data from php://input, mocked in tests.
Returns
contents of php://input
_url() protected ¶
_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
Returns
URI The CakePHP request path that is being accessed.
acceptLanguage() public static ¶
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'); }}}
Parameters
- string $language optional null
- The language to test.
Returns
a $language is provided, a boolean. Otherwise the array of accepted languages.
accepts() public ¶
accepts( string $type = null )
Find out which content types the client accepts or check if they accept a particular type of content.
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.
Parameters
- string $type optional null
- The content type to check for. Leave null to get all types a client accepts.
Returns
Either an array of all the types the client accepts or a boolean if they accept the provided type.
addDetector() public ¶
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.
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('post', array('param' => 'requested', 'value' => 1)
Parameters
- string $name
- The name of the detector.
- array $options
- The options for the detector definition. See above.
addParams() public ¶
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
.
Parameters
- array $params
- Array of parameters to merge in
Returns
current object, you can chain this method.
addPaths() public ¶
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.
Parameters
- array $paths
- Array of paths to merge in
Returns
clientIp() public ¶
clientIp( boolean $safe = true )
Get the IP the client is using, or says they are using.
Parameters
- boolean $safe optional true
Use safe = false when you think the user might manipulate their HTTP_CLIENT_IP header. Setting $safe = false will will also look at HTTP_X_FORWARDED_FOR
Returns
The client IP.
data() public ¶
data( string $name )
Provides a read/write accessor for $this->data
. Allows you
to use a syntax similar to CakeSession
for reading post data.
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.
Parameters
- string $name
- Dot separated name of the value to read/write
Returns
Either the value being read, or this so you can chain consecutive writes.
domain() public ¶
domain( integer $tldLength = 1 )
Get the domain name and include $tldLength segments of the tld.
Parameters
- integer $tldLength optional 1
Number of segments your tld contains. For example:
example.com
contains 1 tld. Whileexample.co.uk
contains 2.
Returns
Domain name without subdomains.
header() public static ¶
header( string $name )
Read an HTTP header from the Request information.
Parameters
- string $name
- Name of the header you want.
Returns
Either false on no header being set or the value of the header.
here() public ¶
here( boolean $base = true )
Get the value of the current requests url. Will include named parameters and querystring arguments.
Parameters
- boolean $base optional true
- Include the base path, set to false to trim the base path off.
Returns
the current request url including query string args.
input() public ¶
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.
Parameters
- string $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.
Returns
decoded/processed request data.
is() public ¶
is( string $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()
.
Parameters
- string $type
- The type of request you want to check.
Returns
Whether or not the request is the type you are checking.
method() public ¶
method( )
Get the HTTP method used for this request. 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.
Returns
The name of the HTTP method used.
offsetExists() public ¶
offsetExists( string $name )
Array access isset() implementation
Parameters
- string $name
- thing to check.
Returns
Implementation of
ArrayAccess::offsetExists()
offsetGet() public ¶
offsetGet( string $name )
Array access read implementation
Parameters
- string $name
- Name of the key being accessed.
Returns
Implementation of
ArrayAccess::offsetGet()
offsetSet() public ¶
offsetSet( string $name , mixed $value )
Array access write implementation
Parameters
- string $name
- Name of the key being written
- mixed $value
- The value being written.
Implementation of
ArrayAccess::offsetSet()
offsetUnset() public ¶
offsetUnset( string $name )
Array access unset() implementation
Parameters
- string $name
- Name to unset.
Implementation of
ArrayAccess::offsetUnset()
parseAccept() public ¶
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.
Returns
An array of prefValue => array(content/types)
referer() public ¶
referer( boolean $local = false )
Returns the referer that referred this request.
Parameters
- boolean $local optional false
- Attempt to return a local address. Local addresses do not contain hostnames.
Returns
The referring address for this request.
subdomains() public ¶
subdomains( integer $tldLength = 1 )
Get the subdomains for a host.
Parameters
- integer $tldLength optional 1
Number of segments your tld contains. For example:
example.com
contains 1 tld. Whileexample.co.uk
contains 2.
Returns
of subdomains.
Properties detail
$_detectors ¶
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'), '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', '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) )
$_input ¶
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.
''
$data ¶
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()