PHP Класс 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.
Наследование: implements ArrayAccess
Показать файл Открыть проект Примеры использования класса

Открытые свойства

Свойство Тип Описание
$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)

Свойство Тип Описание
$_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.

Открытые методы

Метод Описание
__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.

Защищенные методы

Метод Описание
_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.

Описание методов

__call() публичный Метод

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
Результат mixed

__construct() публичный Метод

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() публичный Метод

Allows access to $this->params['controller'] via $this->controller
public __get ( string $name ) : mixed
$name string The property being accessed.
Результат mixed Either the value of the parameter or null.

__isset() публичный Метод

Magic isset method allows isset/empty checks on routing parameters.
public __isset ( string $name ) : boolean
$name string The property being accessed.
Результат boolean Existence

_acceptHeaderDetector() защищенный Метод

Detects if a specific accept header is present.
protected _acceptHeaderDetector ( array $detect ) : boolean
$detect array Detector options array.
Результат boolean Whether or not the request is the type you are checking.

_base() защищенный Метод

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
Результат string Base URL

_environmentDetector() защищенный Метод

Detects if a specific environment variable is present.
protected _environmentDetector ( array $detect ) : boolean
$detect array Detector options array.
Результат boolean Whether or not the request is the type you are checking.

_extensionDetector() защищенный Метод

Detects if a URL extension is present.
protected _extensionDetector ( array $detect ) : boolean
$detect array Detector options array.
Результат boolean Whether or not the request is the type you are checking.

_headerDetector() защищенный Метод

Detects if a specific header is present.
protected _headerDetector ( array $detect ) : boolean
$detect array Detector options array.
Результат boolean Whether or not the request is the type you are checking.

_paramDetector() защищенный Метод

Detects if a specific request parameter is present.
protected _paramDetector ( array $detect ) : boolean
$detect array Detector options array.
Результат boolean Whether or not the request is the type you are checking.

_parseAcceptWithQualifier() защищенный статический Метод

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.
Результат array

_processFileData() защищенный Метод

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.
Результат void

_processFiles() защищенный Метод

Process $_FILES and move things into the object.
protected _processFiles ( ) : void
Результат void

_processGet() защищенный Метод

Process the GET parameters and move things into the object.
protected _processGet ( ) : void
Результат void

_processPost() защищенный Метод

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
Результат void

_readInput() защищенный Метод

Read data from php://input, mocked in tests.
protected _readInput ( ) : string
Результат string contents of php://input

_url() защищенный Метод

Each of these server variables have the base path, and query strings stripped off
protected _url ( ) : string
Результат string URI The CakePHP request path that is being accessed.

acceptLanguage() публичный статический Метод

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.
Результат mixed If a $language is provided, a boolean. Otherwise the array of accepted languages.

accepts() публичный Метод

#### 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.
Результат mixed Either an array of all the types the client accepts or a boolean if they accept the provided type.

addDetector() публичный Метод

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.
Результат void

addParams() публичный Метод

This modifies the parameters available through $request->params.
public addParams ( array $params ) : self
$params array Array of parameters to merge in
Результат self

addPaths() публичный Метод

Provides an easy way to modify, here, webroot and base.
public addPaths ( array $paths ) : self
$paths array Array of paths to merge in
Результат self

allowMethod() публичный Метод

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.
Результат boolean true

clientIp() публичный Метод

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
Результат string The client IP.

contentType() публичный Метод

Get the content type used in this request.
public contentType ( ) : string
Результат string

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.
public data ( string $name ) : mixed | self
$name string Dot separated name of the value to read/write, one or more args.
Результат mixed | self Either the value being read, or $this so you can chain consecutive writes.

domain() публичный Метод

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.
Результат string Domain name without subdomains.

header() публичный статический Метод

Read an HTTP header from the Request information.
public static header ( string $name ) : mixed
$name string Name of the header you want.
Результат mixed Either false on no header being set or the value of the header.

here() публичный Метод

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.
Результат string the current request URL including query string args.

host() публичный Метод

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.
Результат string

input() публичный Метод

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.
Результат The decoded/processed request data.

is() публичный Метод

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.
Результат boolean Whether or not the request is the type you are checking.

isAll() публичный Метод

Allows you to test multiple types and union the results. See CakeRequest::is() for how to add additional types and the built-in types.
См. также: CakeRequest::is()
public isAll ( array $types ) : boolean
$types array The types to check.
Результат boolean Success.

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
Результат string The name of the HTTP method used.

normalizedHere() публичный Метод

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

offsetExists() публичный Метод

Array access isset() implementation
public offsetExists ( string $name ) : boolean
$name string thing to check.
Результат boolean

offsetGet() публичный Метод

Array access read implementation
public offsetGet ( string $name ) : mixed
$name string Name of the key being accessed.
Результат mixed

offsetSet() публичный Метод

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.
Результат void

offsetUnset() публичный Метод

Array access unset() implementation
public offsetUnset ( string $name ) : void
$name string Name to unset.
Результат void

onlyAllow() публичный Метод

Alias of CakeRequest::allowMethod() for backwards compatibility.
См. также: CakeRequest::allowMethod()
Устаревший: 3.0.0 Since 2.5, use CakeRequest::allowMethod() instead.
public onlyAllow ( string | array $methods ) : boolean
$methods string | array Allowed HTTP request methods.
Результат boolean true

param() публичный Метод

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

parseAccept() публичный Метод

Generally you want to use CakeRequest::accept() to get a simple list of the accepted content types.
public parseAccept ( ) : array
Результат array An array of prefValue => array(content/types)

query() публичный Метод

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
Результат mixed The value being read

referer() публичный Метод

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.
Результат string The referring address for this request.

setInput() публичный Метод

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()
Результат void

subdomains() публичный Метод

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.
Результат array An array of subdomains.

Описание свойств

$_detectors защищенное свойство

There are several ways to specify a detector, see CakeRequest::addDetector() for the various formats and ways to define detectors.
protected array $_detectors
Результат array

$_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.
protected string $_input
Результат string

$base публичное свойство

Base URL path.
public string $base
Результат string

$data публичное свойство

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
Результат array

$here публичное свойство

The full address to the current request
public string $here
Результат string

$params публичное свойство

Array of parameters parsed from the URL.
public array $params
Результат array

$query публичное свойство

Array of querystring arguments
public array $query
Результат array

$url публичное свойство

The URL string used for the request.
public string $url
Результат string

$webroot публичное свойство

webroot path segment for the request.
public string $webroot
Результат string