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.
$_inputTypeMap
protected 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
protected string
$_viewClassMap
protected array
A mapping between type and viewClass By default only JSON and XML are mapped, use RequestHandlerComponent::viewClassMap()
$ajaxLayout
public string
$enabled
public boolean
$ext
public string
$request
public $response
public Set 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. Type with the highest weight will be set. If the highest weight has more then one type matching the extensions, the order in which extensions are specified determines which type will be set.
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. Unlike CakeRequest::accepts() this method deals entirely with mapped content types.
Add a new mapped input type. Mapped input types are automatically converted by RequestHandlerComponent during the startup() callback.
Handles (fakes) redirects for Ajax requests using requestAction() Modifies the $_POST and $_SERVER['REQUEST_METHOD'] to simulate a new GET request.
Checks 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.
Helper method to parse xml input data, due to lack of anonymous functions this lives here.
Gets Prototype version if call is Ajax, otherwise empty string. The Prototype library sets a special "Prototype version" HTTP header.
Checks 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.
Returns true if user agent string matches a mobile web browser, or if the client accepts WAP content.
Determines 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.
Sets 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.
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.
The startup method of the RequestHandler enables several automatic behaviors related to the detection of certain properties of the HTTP request, including:
__construct( ComponentCollection $collection , array $settings array() )
Constructor. Parses the accepted content types accepted by the client using HTTP_ACCEPT
ComponentCollection
$collection
$settings
optional array() Component::__construct()
_setExtension( )
Set 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. Type with the highest weight will be set. If the highest weight has more then one type matching the extensions, the order in which extensions are specified determines which type will be set.
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.
accepts( string|array $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. 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.
$type
optional null Can be null (or no parameter), a string type name, or an array of types
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.
addInputType( string $type , array $handler )
Add a new mapped input type. Mapped input types are automatically converted by RequestHandlerComponent during the startup() callback.
$type
$handler
The handler array for the type. The first index should be the handling callback, all other arguments should be additional parameters for the handler.
CakeException
beforeRedirect( Controller $controller , string|array $url , integer|array $status null , boolean $exit true )
Handles (fakes) redirects for Ajax requests using requestAction() Modifies the $_POST and $_SERVER['REQUEST_METHOD'] to simulate a new GET request.
Controller
$controller
$url
$status
optional null $exit
optional true true
.Component::beforeRedirect()
beforeRender( Controller $controller )
Checks 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.
Controller
$controller
Component::beforeRender()
convertXml( string $xml )
Helper method to parse xml input data, due to lack of anonymous functions this lives here.
$xml
getAjaxVersion( )
Gets Prototype version if call is Ajax, otherwise empty string. The Prototype library sets a special "Prototype version" HTTP header.
getClientIP( boolean $safe true )
Gets remote client IP
$safe
optional true 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
getReferer( )
Gets the server name from which this request was referred
initialize( Controller $controller )
Checks 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.
Controller
$controller
Component::initialize()
isAjax( )
Returns true if the current HTTP request is Ajax, false otherwise
$this->request->is('ajax')
instead.isAtom( )
Returns true if the current call accepts an Atom response, false otherwise
isDelete( )
Returns true if the current call a DELETE request
isFlash( )
Returns true if the current HTTP request is coming from a Flash-based client
$this->request->is('flash')
instead.isGet( )
Returns true if the current call a GET request
isMobile( )
Returns true if user agent string matches a mobile web browser, or if the client accepts WAP content.
isPost( )
Returns true if the current call a POST request
isPut( )
Returns true if the current call a PUT request
isRss( )
Returns true if the current call accepts an RSS response, false otherwise
isSSL( )
Returns true if the current request is over HTTPS, false otherwise.
$this->request->is('ssl')
instead.isXml( )
Returns true if the current call accepts an XML response, false otherwise
mapAlias( string|array $alias )
Maps a content type alias back to its mime-type(s)
$alias
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( string|array $cType )
Maps a content-type back to an alias
$cType
prefers( string|array $type null )
Determines 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.
$type
optional null An optional array of 'friendly' content-type names, i.e. 'html', 'xml', 'js', etc.
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.
renderAs( Controller $controller , string $type , array $options array() )
Sets the layout and template paths for the content type defined by $type.
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');
Controller
$controller
$type
$options
optional array() requestedWith( string|array $type null )
Determines the content type of the data the client has sent (i.e. in a POST request)
$type
optional null 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( string|array $type , array $options array() )
Sets 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.
$type
Friendly type name, i.e. 'html' or 'xml', or a full content-type, like 'application/x-shockwave'.
$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 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.
responseType( )
Returns the current response type (Content-type header), or null if not alias exists
A string content type alias, or raw content type if no alias map exists, otherwise null
setContent( string $name , string|array $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.
$this->response->type()
instead.$name
$type
optional null The Content-type or array of Content-types assigned to the name, i.e. "text/html", or "application/xml"
startup( Controller $controller )
The startup method of the RequestHandler enables several automatic behaviors related to the detection of certain properties of the HTTP request, including:
controller/action.xml
is requested, the view path becomes app/View/Controller/xml/action.ctp
. Also if controller/action
is requested with Accept-Type: application/xml
in the headers the view path will become app/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.Controller
$controller
Component::startup()
viewClassMap( array|string $type null , array $viewClass null )
Getter/setter for viewClassMap
$type
optional null array('type' => 'viewClass')
to map one or more$viewClass
optional null View
appended__get( string $name )
Magic method for lazy loading $components.
$name
shutdown( Controller $controller )
Called after Controller::render() and before the output is printed to the browser.
Controller
$controller
_mergeVars( array $properties , string $class , boolean $normalize true )
Merges 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.
$properties
$class
$normalize
optional true _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.
$properties
optional array() _stop( integer|string $status 0 )
Stop execution of the current script. Wraps exit() making testing easier.
$status
optional 0 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
$method
$params
optional array() log( string $msg , integer $type LOG_ERR , null|string|array $scope null )
Convenience method to write a message to CakeLog. See CakeLog::write() for more information on writing to logs.
$msg
$type
optional LOG_ERR $scope
optional null The scope(s) a log message is being created in. See CakeLog::config() for more information on logging scopes.
requestAction( string|array $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.
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()
POST and GET data can be simulated in requestAction. Use $extra['url']
for GET data. The $extra['data']
parameter allows POST data simulation.
$url
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.
$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.
Boolean true or false on success/failure, or contents of rendered action if 'return' is set in $extra.
toString( )
CakeObject-to-string conversion. Each class can override this method as necessary.
protected array
A mapping between extensions and deserializers for request bodies of that type. By default only JSON and XML are mapped, use RequestHandlerComponent::addInputType()
array( 'json' => array('json_decode', true) )
protected array
A mapping between type and viewClass By default only JSON and XML are mapped, use RequestHandlerComponent::viewClassMap()
array( 'json' => 'Json', 'xml' => 'Xml' )
public string
The layout that will be switched to for Ajax requests
'ajax'
public boolean
Determines whether or not callbacks will be fired on this component
true
public string
Contains the file extension parsed out by the Router
null
© 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.
https://api.cakephp.org/2.9/class-RequestHandlerComponent.html