Class RequestHandlerComponent
Request object for handling HTTP requests
- Object
- RequestHandlerComponent
Link: http://book.cakephp.org/1.3/en/The-Manual/Core-Components/Request-Handling.html
Copyright: Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
License: License (http://www.opensource.org/licenses/mit-license.php)
Location: controller/components/request_handler.php
Properties summary
-
$__acceptTypes
publicarray
Content-types accepted by the client. If extension parsing is enabled in the Router, and an extension is detected, the corresponding content-type will be used as the overriding primary content-type accepted.
-
$__renderType
publicstring
The template to use when rendering the given content type. -
$__requestContent
publicarray
Friendly content-type mappings used to set response types and determine request types. Can be modified with RequestHandler::setContent()
-
$__responseTypeSet
publicstring
Holds the content-type of the response that is set when using RequestHandler::respondAs()
-
$__typesInitialized
publicboolean
Flag set when MIME types have been initialized -
$ajaxLayout
publicstring
The layout that will be switched to for Ajax requests -
$enabled
publicboolean
Determines whether or not callbacks will be fired on this component -
$ext
publicstring
Contains the file extension parsed out by the Router -
$mobileUA
publicarray
List of regular expressions for matching mobile device's user agent string -
$params
publicarray
Holds the copy of Controller::$params
Method Summary
-
__construct() public
Constructor. Parses the accepted content types accepted by the client using HTTP_ACCEPT -
__initializeTypes() public
Initializes MIME types -
_header() public
Wrapper for header() so calls can be easily tested. -
accepts() public
Determines which content types the client accepts. Acceptance is based on the file extension parsed by the Router (if present), and by the HTTP_ACCEPT header.
-
beforeRedirect() public
Handles (fakes) redirects for Ajax requests using requestAction() -
getAjaxVersion() public
Gets Prototype version if call is Ajax, otherwise empty string. The Prototype library sets a special "Prototype version" HTTP header.
-
getClientIP() public
Gets remote client IP -
getReferer() public
Gets the server name from which this request was referred -
initialize() public
Initializes the component, gets a reference to Controller::$parameters, and checks to see if a file extension has been parsed by the Router. If yes, the corresponding content-type is pushed onto the list of accepted content-types as the first item.
-
isAjax() public
Returns true if the current HTTP request is Ajax, false otherwise -
isAtom() public
Returns true if the current call accepts an Atom response, false otherwise -
isDelete() public
Returns true if the current call a DELETE request -
isFlash() public
Returns true if the current HTTP request is coming from a Flash-based client -
isGet() public
Returns true if the current call a GET request -
isMobile() public deprecated
Returns true if user agent string matches a mobile web browser, or if the client accepts WAP content.
-
isPost() public
Returns true if the current call a POST request -
isPut() public
Returns true if the current call a PUT request -
isRss() public
Returns true if the current call accepts an RSS response, false otherwise -
isSSL() public
Returns true if the current request is over HTTPS, false otherwise. -
isWap() public
Returns true if the client accepts WAP content -
isXml() public
Returns true if the current call accepts an XML response, false otherwise -
mapType() public
Maps a content-type back to an alias -
prefers() public
Determines which content-types the client prefers. If no parameters are given, the content-type that the client most likely prefers is returned. If $type is an array, the first item in the array that the client accepts is returned. Preference is determined primarily by the file extension parsed by the Router if provided, and secondarily by the list of content-types provided in HTTP_ACCEPT.
-
renderAs() public
Sets the layout and template paths for the content type defined by $type. -
requestedWith() public
Determines the content type of the data the client has sent (i.e. in a POST request) -
respondAs() public
Sets the response header based on type map index name. If DEBUG is greater than 2, the header is not set.
-
responseType() public
Returns the current response type (Content-type header), or null if none has been set -
setContent() public
Adds/sets the Content-type(s) for the given name. This method allows content-types to be mapped to friendly aliases (or extensions), which allows RequestHandler to automatically respond to requests of that type in the startup method.
-
startup() public
The startup method of the RequestHandler enables several automatic behaviors related to the detection of certain properties of the HTTP request, including:
Method Detail
__construct() public ¶
__construct( )
Constructor. Parses the accepted content types accepted by the client using HTTP_ACCEPT
Overrides
_header() public ¶
_header( string $header )
Wrapper for header() so calls can be easily tested.
Parameters
- string $header
- The header to be sent.
accepts() public ¶
accepts( mixed $type = null )
Determines which content types the client accepts. Acceptance is based on the file extension parsed by the Router (if present), and by the HTTP_ACCEPT header.
Parameters
- mixed $type optional null
Can be null (or no parameter), a string type name, or an array of types
Returns
If null or no parameter is passed, returns an array of content types the client accepts. If a string is passed, returns true if the client accepts it. If an array is passed, returns true if the client accepts one or more elements in the array.
See
beforeRedirect() public ¶
beforeRedirect( object $controller , mixed $url , mixed $status = null )
Handles (fakes) redirects for Ajax requests using requestAction()
Parameters
- object $controller
- A reference to the controller
- mixed $url
- A string or array containing the redirect location
- mixed $status optional null
- Status for redirect
getAjaxVersion() public ¶
getAjaxVersion( )
Gets Prototype version if call is Ajax, otherwise empty string. The Prototype library sets a special "Prototype version" HTTP header.
Returns
Prototype version of component making Ajax call
getClientIP() public ¶
getClientIP( $safe = true )
Gets remote client IP
Returns
Client IP address
getReferer() public ¶
getReferer( )
Gets the server name from which this request was referred
Returns
Server address
initialize() public ¶
initialize( object $controller , array $settings = array() )
Initializes the component, gets a reference to Controller::$parameters, and checks to see if a file extension has been parsed by the Router. If yes, the corresponding content-type is pushed onto the list of accepted content-types as the first item.
Parameters
- object $controller
- A reference to the controller
- array $settings optional array()
- Array of settings to _set().
See
isAjax() public ¶
isAjax( )
Returns true if the current HTTP request is Ajax, false otherwise
Returns
True if call is Ajax
isAtom() public ¶
isAtom( )
Returns true if the current call accepts an Atom response, false otherwise
Returns
True if client accepts an RSS response
isDelete() public ¶
isDelete( )
Returns true if the current call a DELETE request
Returns
True if call is a DELETE
isFlash() public ¶
isFlash( )
Returns true if the current HTTP request is coming from a Flash-based client
Returns
True if call is from Flash
isGet() public ¶
isGet( )
Returns true if the current call a GET request
Returns
True if call is a GET
isMobile() public deprecated ¶
isMobile( )
Returns true if user agent string matches a mobile web browser, or if the client accepts WAP content.
Deprecated
Returns
True if user agent is a mobile web browser
isPost() public ¶
isPost( )
Returns true if the current call a POST request
Returns
True if call is a POST
isPut() public ¶
isPut( )
Returns true if the current call a PUT request
Returns
True if call is a PUT
isRss() public ¶
isRss( )
Returns true if the current call accepts an RSS response, false otherwise
Returns
True if client accepts an RSS response
isSSL() public ¶
isSSL( )
Returns true if the current request is over HTTPS, false otherwise.
Returns
True if call is over HTTPS
isXml() public ¶
isXml( )
Returns true if the current call accepts an XML response, false otherwise
Returns
True if client accepts an XML response
mapType() public ¶
mapType( mixed $ctype )
Maps a content-type back to an alias
Parameters
- mixed $ctype
- $type Content type
Returns
Alias
prefers() public ¶
prefers( mixed $type = null )
Determines which content-types the client prefers. If no parameters are given, the content-type that the client most likely prefers is returned. If $type is an array, the first item in the array that the client accepts is returned. Preference is determined primarily by the file extension parsed by the Router if provided, and secondarily by the list of content-types provided in HTTP_ACCEPT.
Parameters
- mixed $type optional null
An optional array of 'friendly' content-type names, i.e. 'html', 'xml', 'js', etc.
Returns
If $type is null or not provided, the first content-type in the list, based on preference, is returned.
See
renderAs() public ¶
renderAs( object $controller , string $type )
Sets the layout and template paths for the content type defined by $type.
Parameters
- object $controller
- A reference to a controller object
- string $type
- Type of response to send (e.g: 'ajax')
See
RequestHandlerComponent::respondAs()
requestedWith() public ¶
requestedWith( mixed $type = null )
Determines the content type of the data the client has sent (i.e. in a POST request)
Parameters
- mixed $type optional null
- Can be null (or no parameter), a string type name, or an array of types
Returns
respondAs() public ¶
respondAs( mixed $type , array $options = array() )
Sets the response header based on type map index name. If DEBUG is greater than 2, the header is not set.
Parameters
- mixed $type
Friendly type name, i.e. 'html' or 'xml', or a full content-type, like 'application/x-shockwave'.
- array $options optional array()
If $type is a friendly type name that is associated with more than one type of content, $index is used to select which content-type to use.
Returns
Returns false if the friendly type name given in $type does not exist in the type map, or if the Content-type header has already been set by this method.
See
responseType() public ¶
responseType( )
Returns the current response type (Content-type header), or null if none has been set
Returns
A string content type alias, or raw content type if no alias map exists, otherwise null
setContent() public ¶
setContent( string $name , mixed $type = null )
Adds/sets the Content-type(s) for the given name. This method allows content-types to be mapped to friendly aliases (or extensions), which allows RequestHandler to automatically respond to requests of that type in the startup method.
Parameters
- string $name
- The name of the Content-type, i.e. "html", "xml", "css"
- mixed $type optional null
The Content-type or array of Content-types assigned to the name, i.e. "text/html", or "application/xml"
startup() public ¶
startup( object $controller )
The startup method of the RequestHandler enables several automatic behaviors related to the detection of certain properties of the HTTP request, including:
- Disabling layout rendering for Ajax requests (based on the HTTP_X_REQUESTED_WITH header)
- If Router::parseExtensions() is enabled, the layout and template type are switched based on the parsed extension. For example, if controller/action.xml is requested, the view path becomes app/views/controller/xml/action.ctp.
- If a helper with the same name as the extension exists, it is added to the controller.
- If the extension is of a type that RequestHandler understands, it will set that Content-type in the response header.
- If the XML data is POSTed, the data is parsed into an XML object, which is assigned to the $data property of the controller, which can then be saved to a model object.
Parameters
- object $controller
- A reference to the controller
Methods inherited from Object
Object() public ¶
Object( )
A hack to support __construct() on PHP 4 Hint: descendant classes have no PHP4 class_name() constructors, so this constructor gets called first and calls the top-layer __construct() which (if present) should call parent::__construct()
Returns
__openPersistent() public ¶
__openPersistent( string $name , string $type = null )
Open the persistent class file for reading Used by Object::_persist()
Parameters
- string $name
- Name of persisted class
- string $type optional null
- Type of persistance (e.g: registry)
_persist() public ¶
_persist( string $name , string $return , $object , $type = null )
Checks for a persistent class file, if found file is opened and true returned If file is not found a file is created and false returned If used in other locations of the model you should choose a unique name for the persistent file There are many uses for this method, see manual for examples
Parameters
- string $name
- name of the class to persist
- string $return
- $object the object to persist
- $object
- $type optional null
Returns
Success
_savePersistent() public ¶
_savePersistent( string $name , object $object )
You should choose a unique name for the persistent file
There are many uses for this method, see manual for examples
Parameters
- string $name
- name used for object to cache
- object $object
- the object to persist
Returns
true on save, throws error if file can not be created
_set() public ¶
_set( array $properties = array() )
Allows setting of multiple properties of the object in a single line of code. Will only set properties that are part of a class declaration.
Parameters
- array $properties optional array()
- An associative array containing properties and corresponding values.
_stop() public ¶
_stop( $status = 0 )
Stop execution of the current script. Wraps exit() making testing easier.
Parameters
- $status optional 0
- http://php.net/exit for values
cakeError() public ¶
cakeError( string $method , array $messages = array() )
Used to report user friendly errors. If there is a file app/error.php or app/app_error.php this file will be loaded error.php is the AppError class it should extend ErrorHandler class.
Parameters
- string $method
- Method to be called in the error class (AppError or ErrorHandler classes)
- array $messages optional array()
- Message that is to be displayed by the error class
Returns
message
dispatchMethod() public ¶
dispatchMethod( string $method , array $params = array() )
Calls a method on this object with the given parameters. Provides an OO wrapper
for call_user_func_array
Parameters
- string $method
- Name of the method to call
- array $params optional array()
- Parameter list to use when calling $method
Returns
Returns the result of the method call
log() public ¶
log( string $msg , integer $type = LOG_ERROR )
Convience method to write a message to CakeLog. See CakeLog::write() for more information on writing to logs.
Parameters
- string $msg
- Log message
- integer $type optional LOG_ERROR
- Error type constant. Defined in app/config/core.php.
Returns
Success of log write
requestAction() public ¶
requestAction( mixed $url , array $extra = array() )
Calls a controller's method from any location. Can be used to connect controllers together or tie plugins into a main application. requestAction can be used to return rendered views or fetch the return value from controller actions.
Parameters
- mixed $url
- String or array-based url.
- array $extra optional array()
- if array includes the key "return" it sets the AutoRender to true.
Returns
Boolean true or false on success/failure, or contents of rendered action if 'return' is set in $extra.
toString() public ¶
toString( )
Object-to-string conversion. Each class can override this method as necessary.
Returns
The name of this class
Properties detail
$__acceptTypes ¶
Content-types accepted by the client. If extension parsing is enabled in the Router, and an extension is detected, the corresponding content-type will be used as the overriding primary content-type accepted.
See
array()
$__requestContent ¶
Friendly content-type mappings used to set response types and determine request types. Can be modified with RequestHandler::setContent()
See
array( 'javascript' => 'text/javascript', 'js' => 'text/javascript', 'json' => 'application/json', 'css' => 'text/css', 'html' => array('text/html', '*/*'), 'text' => 'text/plain', 'txt' => 'text/plain', 'csv' => array('text/csv', 'application/vnd.ms-excel', 'text/plain'), 'form' => 'application/x-www-form-urlencoded', 'file' => 'multipart/form-data', 'xhtml' => array('application/xhtml+xml', 'application/xhtml', 'text/xhtml'), 'xhtml-mobile' => 'application/vnd.wap.xhtml+xml', 'xml' => array('application/xml', 'text/xml'), 'rss' => 'application/rss+xml', 'atom' => 'application/atom+xml', 'amf' => 'application/x-amf', 'wap' => array( 'text/vnd.wap.wml', 'text/vnd.wap.wmlscript', 'image/vnd.wap.wbmp' ), 'wml' => 'text/vnd.wap.wml', 'wmlscript' => 'text/vnd.wap.wmlscript', 'wbmp' => 'image/vnd.wap.wbmp', 'pdf' => 'application/pdf', 'zip' => 'application/x-zip', 'tar' => 'application/x-tar' )
$__responseTypeSet ¶
Holds the content-type of the response that is set when using RequestHandler::respondAs()
null
$__typesInitialized ¶
Flag set when MIME types have been initialized
See
false
$ajaxLayout ¶
The layout that will be switched to for Ajax requests
See
'ajax'
$ext ¶
Contains the file extension parsed out by the Router
See
null
$mobileUA ¶
List of regular expressions for matching mobile device's user agent string
array( 'Android', 'AvantGo', 'BlackBerry', 'DoCoMo', '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' )