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
.
__call( string $name , array $params )
Missing method handler, handles wrapping older style isAjax() type methods
$name
$params
mixed
BadMethodCallException
__construct( string|array $config [] )
Create a new request object.
You can supply the data as either an array or as a string. If you use a string you can only supply the URL for the request. Using an array will let you provide the following keys:
post
POST data or non query string dataquery
Additional data from the query string.files
Uploaded file data formatted like $_FILES.cookies
Cookies for this request.environment
$_SERVER and $_ENV data.url
The URL without the base path for the request.base
The base URL for the request.webroot
The webroot directory for the request.input
The data that would come from php://input this is useful for simulatingsession
An instance of a Session object requests with put, patch or delete data.$config
optional [] __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
mixed
__isset( string $name )
Magic isset method allows isset/empty checks on routing parameters.
$name
boolean
_acceptHeaderDetector( array $detect )
Detects if a specific accept header is present.
$detect
boolean
_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.
array
_environmentDetector( array $detect )
Detects if a specific environment variable is present.
$detect
boolean
_headerDetector( array $detect )
Detects if a specific header is present.
$detect
boolean
_is( string|array $type )
Worker for the public is() function
$type
boolean
_paramDetector( array $detect )
Detects if a specific request parameter is present.
$detect
boolean
_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
array
_processFileData( array $data , array $post , string $path '' , string $field '' )
Recursively walks the FILES array restructuring the data into something sane and usable.
$data
$post
$path
optional '' $field
optional '' array
_processFiles( array $post , array $files )
Process uploaded files and move things onto the post data.
$post
$files
array
_processGet( array $query )
Process the GET parameters and move things into the object.
$query
array
_processPost( array $data )
Sets the REQUEST_METHOD environment variable based on the simulated _method HTTP override value. The 'ORIGINAL_REQUEST_METHOD' is also preserved, if you want the read the non-simulated HTTP method the client used.
$data
array
_readInput( )
Read data from php://input, mocked in tests.
string
_setConfig( array $config )
Process the config/settings data into properties.
$config
_url( array $config )
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
$config
string
acceptLanguage( string|null $language null )
Get the languages accepted by the client, or check if a specific language is accepted.
Get the list of accepted languages:
\Cake\Network\Request::acceptLanguage();
Check if a specific language is accepted:
\Cake\Network\Request::acceptLanguage('es-es');
$language
optional null array|boolean
accepts( string|null $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 array|boolean
addDetector( string $name , callable|array $callable )
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.
Callback detectors allow you to provide a callable to handle the check. The callback will receive the request object as its only parameter.
addDetector('custom', function ($request) { //Return a boolean }); addDetector('custom', ['SomeClass', 'somemethod']);
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', ['env' => 'REQUEST_METHOD', 'value' => 'POST'])
Pattern value comparison allows you to compare a value fetched from env()
to a regular expression.
addDetector('iphone', ['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.
addDetector('mobile', ['env' => 'HTTP_USER_AGENT', 'options' => ['Fennec']]);
Allows for custom detectors on the request parameters.
e.g addDetector('requested', ['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', ['param' => 'ext', 'options' => ['pdf', 'csv']]
$name
$callable
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
mixed
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
mixed
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'); or $this->request->allowMethod(['post', 'delete']);
If the request would be GET, response header "Allow: POST, DELETE" will be set and a 405 error will be returned.
$methods
boolean
Cake\Network\Exception\MethodNotAllowedException
clearDetectorCache( )
Clears the instance detector cache, used by the is() function
clientIp( )
Get the IP the client is using, or says they are using.
string
contentType( )
Get the content type used in this request.
string
cookie( string $key )
Read cookie data from the request's cookie data.
$key
null|string
createFromGlobals( )
Wrapper method to create a new request from PHP superglobals.
Uses the $_GET, $_POST, $_FILES, $_COOKIE, $_SERVER, $_ENV and php://input data to construct the request.
Cake\Network\Request
data( string|null $name null )
Provides a read/write accessor for $this->data
. Allows you to use a syntax similar to Cake\Model\Datasource\Session
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
optional null mixed|$this
domain( integer $tldLength 1 )
Get the domain name and include $tldLength segments of the tld.
$tldLength
optional 1 example.com
contains 1 tld. While example.co.uk
contains 2.string
env( string $key , string|null $value null , string|null $default null )
Get/Set value from the request's environment data. Fallback to using env() if key not set in $environment property.
$key
$value
optional null $default
optional null mixed
header( string $name )
Read an HTTP header from the Request information.
$name
string|null
here( boolean $base true )
Get the value of the current requests URL. Will include querystring arguments.
$base
optional true string
input( string|null $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', ['return' => 'DOMDocument']);
Any additional parameters are applied to the callback in the order they are given.
$callback
optional null string
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 Cake\Network\CakeRequest::addDetector(). Any detector can be called as is($type)
or is$Type()
.
$type
boolean
isAll( array $types )
Check that a request matches all the given types.
Allows you to test multiple types and union the results. See Request::is() for how to add additional types and the built-in types.
$types
boolean
Cake\Network\Request::is()
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.
string
offsetExists( string $name )
Array access isset() implementation
$name
boolean
ArrayAccess::offsetExists()
offsetGet( string $name )
Array access read implementation
$name
mixed
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()
param( string $name )
Safely access the values in $this->params.
$name
mixed|$this
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 Cake\Network\Request::accept() to get a simple list of the accepted content types.
array
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
mixed
referer( boolean $local false )
Returns the referer that referred this request.
$local
optional false string
scheme( )
Get the current url scheme used for the request.
e.g. 'http', or 'https'
string
session( Cake\Network\Session $session null )
Returns the instance of the Session object for this request
If a session object is passed as first argument it will be set as the session to use for this request
Cake\Network\Session
$session
optional null Cake\Network\Session
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 example.com
contains 1 tld. While example.co.uk
contains 2.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.
http://api.cakephp.org/3.1/class-Cake.Network.Request.html