RequestHandlerComponent Class Info:
- Class Declaration:
class RequestHandlerComponent extends Component
- File name:
- Cake/Controller/Component/RequestHandlerComponent.php
- Description:
Request object for handling alternative HTTP requests
Alternative HTTP requests can come from wireless units like mobile phones, palmtop computers, and the like. These units have no use for Ajax requests, and this Component can tell how Cake should respond to the different needs of a handheld computer and a desktop machine.
- Class Inheritance
Properties:
-
ajaxLayout string
The layout that will be switched to for Ajax requests
-
_Collection ComponentCollection
Component collection class used to lazy load components.
-
_componentMap array
A component lookup table used to lazy load component objects.
-
components array
Other Components this component uses.
-
enabled boolean
Determines whether or not callbacks will be fired on this component
-
ext string
Contains the file extension parsed out by the Router
-
_inputTypeMap array
A mapping between extensions and deserializers for request bodies of that type. By default only JSON and XML are mapped, use RequestHandlerComponent::addInputType()
-
_renderType string
The template to use when rendering the given content type.
-
request CakeRequest
Holds the reference to Controller::$request
-
response CakeResponse
Holds the reference to Controller::$response
-
settings array
Settings for this Component
Method Summary:
- accepts( $type = NULL )
- addInputType( $type, $handler )
- beforeRedirect( $controller, $url, $status = NULL, $exit = true )
- beforeRender( $controller )
- __construct( $collection, $settings = array ( ) )
- convertXml( $xml )
- dispatchMethod( $method, $params = array ( ) )
- __get( $name )
- getAjaxVersion( )
- getClientIP( $safe = true )
- getReferer( )
- initialize( $controller, $settings = array ( ) )
- isAjax( )
- isAtom( )
- isDelete( )
- isFlash( )
- isGet( )
- isMobile( )
- isPost( )
- isPut( )
- isRss( )
- isSSL( )
- isWap( )
- isXml( )
- log( $msg, $type = 2 )
- mapAlias( $alias )
- mapType( $cType )
- _mergeVars( $properties, $class, $normalize = true )
- prefers( $type = NULL )
- renderAs( $controller, $type, $options = array ( ) )
- requestAction( $url, $extra = array ( ) )
- requestedWith( $type = NULL )
- respondAs( $type, $options = array ( ) )
- responseType( )
- _set( $properties = array ( ) )
- setContent( $name, $type = NULL )
- _setExtension( )
- shutdown( $controller )
- startup( $controller )
- _stop( $status = 0 )
- toString( )
accepts
topDetermines 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. Unlike CakeRequest::accepts() this method deals entirely with mapped content types.
Usage:
$this->RequestHandler->accepts(array('xml', 'html', 'json'));
Returns true if the client accepts any of the supplied types.
$this->RequestHandler->accepts('xml');
Returns true if the client accepts xml.
- Parameters:
-
-
mixed $type optional NULL
Can be null (or no parameter), a string type name, or an array of types
-
- Method defined in:
- Cake/Controller/Component/RequestHandlerComponent.php on line 452
- Return
mixed 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
addInputType
topAdd a new mapped input type. Mapped input types are automatically converted by RequestHandlerComponent during the startup() callback.
- Parameters:
-
-
string $type required
The type alias being converted, ie. json
-
array $handler required
The handler array for the type. The first index should be the handling callback, all other arguments should be additional parameters for the handler.
-
- Method defined in:
- Cake/Controller/Component/RequestHandlerComponent.php on line 723
- Return
void
- Throws
CakeException
beforeRedirect
topHandles (fakes) redirects for Ajax requests using requestAction()
- Parameters:
-
-
Controller $controller required
A reference to the controller
-
string|array $url required
A string or array containing the redirect location
-
mixed $status optional NULL
HTTP Status for redirect
-
boolean $exit optional true
HTTP Status for redirect
-
- Method defined in:
- Cake/Controller/Component/RequestHandlerComponent.php on line 227
- Return
void
beforeRender
topChecks if the response can be considered different according to the request headers, and the caching response headers. If it was not modified, then the render process is skipped. And the client will get a blank response with a "304 Not Modified" header.
- Parameters:
-
-
$controller required
-
- Method defined in:
- Cake/Controller/Component/RequestHandlerComponent.php on line 256
- Params
Controller $controller
- Return
boolean false if the render process should be aborted
__construct
topConstructor. Parses the accepted content types accepted by the client using HTTP_ACCEPT
- Parameters:
-
-
ComponentCollection $collection required
ComponentCollection object.
-
array $settings optional array ( )
Array of settings.
-
- Method defined in:
- Cake/Controller/Component/RequestHandlerComponent.php on line 97
convertXml
topHelper method to parse xml input data, due to lack of anonymous functions this lives here.
- Parameters:
-
-
string $xml required
-
- Method defined in:
- Cake/Controller/Component/RequestHandlerComponent.php on line 206
- Return
array Xml array data
dispatchMethod
topCalls a method on this object with the given parameters. Provides an OO wrapper
for call_user_func_array
- Parameters:
-
-
string $method required
Name of the method to call
-
array $params optional array ( )
Parameter list to use when calling $method
-
- Method defined in:
- Cake/Core/Object.php on line 110
- Return
mixed Returns the result of the method call
__get
topMagic method for lazy loading $components.
- Parameters:
-
-
string $name required
Name of component to get.
-
- Method defined in:
- Cake/Controller/Component.php on line 92
- Return
mixed A Component object or null.
getAjaxVersion
topGets Prototype version if call is Ajax, otherwise empty string. The Prototype library sets a special "Prototype version" HTTP header.
- Method defined in:
- Cake/Controller/Component/RequestHandlerComponent.php on line 385
- Return
string Prototype version of component making Ajax call
getClientIP
topGets remote client IP
- Parameters:
-
-
boolean $safe optional true
-
- Method defined in:
- Cake/Controller/Component/RequestHandlerComponent.php on line 425
- Return
string Client IP address
- Deprecated
use $this->request->clientIp() from your, controller instead.
getReferer
topGets the server name from which this request was referred
- Method defined in:
- Cake/Controller/Component/RequestHandlerComponent.php on line 414
- Return
string Server address
- Deprecated
use $this->request->referer() from your controller instead
initialize
topChecks to see if a file extension has been parsed by the Router, or if the HTTP_ACCEPT_TYPE has matches only one content type with the supported extensions. If there is only one matching type between the supported content types & extensions, and the requested mime-types, RequestHandler::$ext is set to that value.
- Parameters:
-
-
Controller $controller required
A reference to the controller
-
array $settings optional array ( )
Array of settings to _set().
-
- Method defined in:
- Cake/Controller/Component/RequestHandlerComponent.php on line 118
- Return
void
- See
isAjax
topReturns true if the current HTTP request is Ajax, false otherwise
- Method defined in:
- Cake/Controller/Component/RequestHandlerComponent.php on line 269
- Return
boolean True if call is Ajax
- Deprecated
use
$this->request->is('ajax')instead.
isAtom
topReturns true if the current call accepts an Atom response, false otherwise
- Method defined in:
- Cake/Controller/Component/RequestHandlerComponent.php on line 316
- Return
boolean True if client accepts an RSS response
isDelete
topReturns true if the current call a DELETE request
- Method defined in:
- Cake/Controller/Component/RequestHandlerComponent.php on line 375
- Return
boolean True if call is a DELETE
- Deprecated
Use $this->request->is('delete'); from your controller.
isFlash
topReturns true if the current HTTP request is coming from a Flash-based client
- Method defined in:
- Cake/Controller/Component/RequestHandlerComponent.php on line 279
- Return
boolean True if call is from Flash
- Deprecated
use
$this->request->is('flash')instead.
isGet
topReturns true if the current call a GET request
- Method defined in:
- Cake/Controller/Component/RequestHandlerComponent.php on line 365
- Return
boolean True if call is a GET
- Deprecated
Use $this->request->is('get'); from your controller.
isMobile
topReturns true if user agent string matches a mobile web browser, or if the client accepts WAP content.
- Method defined in:
- Cake/Controller/Component/RequestHandlerComponent.php on line 326
- Return
boolean True if user agent is a mobile web browser
isPost
topReturns true if the current call a POST request
- Method defined in:
- Cake/Controller/Component/RequestHandlerComponent.php on line 345
- Return
boolean True if call is a POST
- Deprecated
Use $this->request->is('post'); from your controller.
isPut
topReturns true if the current call a PUT request
- Method defined in:
- Cake/Controller/Component/RequestHandlerComponent.php on line 355
- Return
boolean True if call is a PUT
- Deprecated
Use $this->request->is('put'); from your controller.
isRss
topReturns true if the current call accepts an RSS response, false otherwise
- Method defined in:
- Cake/Controller/Component/RequestHandlerComponent.php on line 307
- Return
boolean True if client accepts an RSS response
isSSL
topReturns true if the current request is over HTTPS, false otherwise.
- Method defined in:
- Cake/Controller/Component/RequestHandlerComponent.php on line 289
- Return
boolean True if call is over HTTPS
- Deprecated
use
$this->request->is('ssl')instead.
isWap
topReturns true if the client accepts WAP content
- Method defined in:
- Cake/Controller/Component/RequestHandlerComponent.php on line 335
- Return
boolean
isXml
topReturns true if the current call accepts an XML response, false otherwise
- Method defined in:
- Cake/Controller/Component/RequestHandlerComponent.php on line 298
- Return
boolean True if client accepts an XML response
log
topConvenience method to write a message to CakeLog. See CakeLog::write() for more information on writing to logs.
- Parameters:
-
-
string $msg required
Log message
-
integer $type optional 2
Error type constant. Defined in app/Config/core.php.
-
- Method defined in:
- Cake/Core/Object.php on line 149
- Return
boolean Success of log write
mapAlias
topMaps a content type alias back to its mime-type(s)
- Parameters:
-
-
mixed $alias required
String alias to convert back into a content type. Or an array of aliases to map.
-
- Method defined in:
- Cake/Controller/Component/RequestHandlerComponent.php on line 698
- Return
mixed Null on an undefined alias. String value of the mapped alias type. If an alias maps to more than one content type, the first one will be returned.
mapType
topMaps a content-type back to an alias
- Parameters:
-
-
mixed $cType required
Either a string content type to map, or an array of types.
-
- Method defined in:
- Cake/Controller/Component/RequestHandlerComponent.php on line 687
- Return
mixed Aliases for the types provided.
- Deprecated
Use $this->response->mapType() in your controller instead.
_mergeVars
topMerges this objects $property with the property in $class' definition. This classes value for the property will be merged on top of $class'
This provides some of the DRY magic CakePHP provides. If you want to shut it off, redefine this method as an empty function.
- Parameters:
-
-
array $properties required
The name of the properties to merge.
-
string $class required
The class to merge the property with.
-
boolean $normalize optional true
Set to true to run the properties through Set::normalize() before merging.
-
- Method defined in:
- Cake/Core/Object.php on line 187
- Return
void
prefers
topDetermines which content-types the client prefers. If no parameters are given, the single 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.
-
- Method defined in:
- Cake/Controller/Component/RequestHandlerComponent.php on line 517
- Return
mixed If $type is null or not provided, the first content-type in the list, based on preference, is returned. If a single type is provided a boolean will be returned if that type is preferred. If an array of types are provided then the first preferred type is returned. If no type is provided the first preferred type is returned.
- See
renderAs
topSets the layout and template paths for the content type defined by $type.
Usage:
Render the response as an 'ajax' response.
$this->RequestHandler->renderAs($this, 'ajax');
Render the response as an xml file and force the result as a file download.
$this->RequestHandler->renderAs($this, 'xml', array('attachment' => 'myfile.xml');
- Parameters:
-
-
Controller $controller required
A reference to a controller object
-
string $type required
Type of response to send (e.g: 'ajax')
-
array $options optional array ( )
Array of options to use
-
- Method defined in:
- Cake/Controller/Component/RequestHandlerComponent.php on line 569
requestAction
topCalls 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.
Under the hood this method uses Router::reverse() to convert the $url parameter into a string URL. You should use URL formats that are compatible with Router::reverse()
Passing POST and GET data
POST and GET data can be simulated in requestAction. Use $extra['url'] for
GET data. The $extra['data'] parameter allows POST data simulation.
- Parameters:
-
-
mixed $url required
String or array-based url. Unlike other url arrays in CakePHP, this url will not automatically handle passed and named arguments in the $url parameter.
-
array $extra optional array ( )
if array includes the key "return" it sets the AutoRender to true. Can also be used to submit GET/POST data, and named/passed arguments.
-
- Method defined in:
- Cake/Core/Object.php on line 66
- Return
mixed Boolean true or false on success/failure, or contents of rendered action if 'return' is set in $extra.
requestedWith
topDetermines 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
-
- Method defined in:
- Cake/Controller/Component/RequestHandlerComponent.php on line 480
- Return
mixed If a single type is supplied a boolean will be returned. If no type is provided The mapped value of CONTENT_TYPE will be returned. If an array is supplied the first type in the request content type will be returned.
respondAs
topSets the response header based on type map index name. This wraps several methods available on CakeResponse. It also allows you to use Content-Type aliases.
- Parameters:
-
-
mixed $type required
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.
-
- Method defined in:
- Cake/Controller/Component/RequestHandlerComponent.php on line 631
- Return
boolean 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
topReturns the current response type (Content-type header), or null if not alias exists
- Method defined in:
- Cake/Controller/Component/RequestHandlerComponent.php on line 676
- Return
mixed A string content type alias, or raw content type if no alias map exists, otherwise null
_set
topAllows 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.
-
- Method defined in:
- Cake/Core/Object.php on line 164
- Return
void
setContent
topAdds/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 required
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"
-
- Method defined in:
- Cake/Controller/Component/RequestHandlerComponent.php on line 404
- Return
void
- Deprecated
use
$this->response->type()instead.
_setExtension
topSet the extension based on the accept headers. Compares the accepted types and configured extensions. If there is one common type, that is assigned as the ext/content type for the response.
If html is one of the preferred types, no content type will be set, this is to avoid issues with browsers that prefer html and several other content types.
- Method defined in:
- Cake/Controller/Component/RequestHandlerComponent.php on line 140
- Return
void
shutdown
topCalled after Controller::render() and before the output is printed to the browser.
- Parameters:
-
-
Controller $controller required
Controller with components to shutdown
-
- Method defined in:
- Cake/Controller/Component.php on line 140
- Return
void
- Link
- @link http://book.cakephp.org/2.0/en/controllers/components.html#Component::shutdown
startup
topThe 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 or Accept-Type header. For example, if
controller/action.xmlis requested, the view path becomesapp/View/Controller/xml/action.ctp. Also ifcontroller/actionis requested withAccept-Type: application/xmlin the headers the view path will becomeapp/View/Controller/xml/action.ctp. Layout and template types will only switch to mime-types recognized by CakeResponse. If you need to declare additional mime-types, you can do so using CakeResponse::type() in your controllers beforeFilter() method. - 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:
-
-
Controller $controller required
A reference to the controller
-
- Method defined in:
- Cake/Controller/Component/RequestHandlerComponent.php on line 176
- Return
void
_stop
topStop execution of the current script. Wraps exit() making testing easier.
- Parameters:
-
-
integer|string $status optional 0
see http://php.net/exit for values
-
- Method defined in:
- Cake/Core/Object.php on line 137
- Return
void
toString
topObject-to-string conversion. Each class can override this method as necessary.
- Method defined in:
- Cake/Core/Object.php on line 41
- Return
string The name of this class
