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.
__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 mixed
RequestHandlerComponent::setContent()
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
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
boolean
Component::beforeRender()
convertXml( string $xml )
Helper method to parse xml input data, due to lack of anonymous functions this lives here.
$xml
array
getAjaxVersion( )
Gets Prototype version if call is Ajax, otherwise empty string. The Prototype library sets a special "Prototype version" HTTP header.
string|boolean
getClientIP( boolean $safe true )
Gets remote client IP
$safe
optional true string
getReferer( )
Gets the server name from which this request was referred
string
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
Router::parseExtensions()
Component::initialize()
isAjax( )
Returns true if the current HTTP request is Ajax, false otherwise
$this->request->is('ajax')
instead.boolean
isAtom( )
Returns true if the current call accepts an Atom response, false otherwise
boolean
isDelete( )
Returns true if the current call a DELETE request
boolean
isFlash( )
Returns true if the current HTTP request is coming from a Flash-based client
$this->request->is('flash')
instead.boolean
isGet( )
Returns true if the current call a GET request
boolean
isMobile( )
Returns true if user agent string matches a mobile web browser, or if the client accepts WAP content.
boolean
isPost( )
Returns true if the current call a POST request
boolean
isPut( )
Returns true if the current call a PUT request
boolean
isRss( )
Returns true if the current call accepts an RSS response, false otherwise
boolean
isSSL( )
Returns true if the current request is over HTTPS, false otherwise.
$this->request->is('ssl')
instead.boolean
isXml( )
Returns true if the current call accepts an XML response, false otherwise
boolean
mapAlias( string|array $alias )
Maps a content type alias back to its mime-type(s)
$alias
string|null
mapType( string|array $cType )
Maps a content-type back to an alias
$cType
string|array
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 mixed
RequestHandlerComponent::setContent()
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() RequestHandlerComponent::setContent()
RequestHandlerComponent::respondAs()
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 mixed
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
$options
optional array() boolean
RequestHandlerComponent::setContent()
responseType( )
Returns the current response type (Content-type header), or null if not alias exists
mixed
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 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
appendedarray|string
__get( string $name )
Magic method for lazy loading $components.
$name
mixed
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() mixed
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 boolean
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
$extra
optional array() mixed
toString( )
Object-to-string conversion. Each class can override this method as necessary.
string
protected ComponentCollection
Component collection class used to lazy load components.
protected array
A component lookup table used to lazy load component objects.
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/2.7/class-RequestHandlerComponent.html