Class RequestHandlerComponent
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.
Link: http://book.cakephp.org/3.0/en/controllers/components/request-handling.html
Property Summary
-
$_componentMap protected
array
A component lookup table used to lazy load component objects.
-
$_config protected
array
Runtime config
-
$_configInitialized protected
bool
Whether the config property has already been configured with defaults
-
$_defaultConfig protected
array
Default config
-
$_registry protected
Cake\Controller\ComponentRegistry
Component registry class used to lazy load components.
-
$_renderType protected
string
The template to use when rendering the given content type.
-
$components public
array
Other Components this component uses.
-
$enabled public
bool
Determines whether or not callbacks will be fired on this component
-
$ext public
string
Contains the file extension parsed out by the Router
-
$request public
Cake\Network\Request
Request object
-
$response public
Cake\Network\Response
Holds the reference to Controller::$response
Method Summary
-
__construct() public
Constructor. Parses the accepted content types accepted by the client using HTTP_ACCEPT
-
__debugInfo() public
Returns an array that can be used to describe the internal state of this object.
-
__get() public
Magic method for lazy loading $components.
-
_configDelete() protected
Delete a single config key
-
_configRead() protected
Read a config variable
-
_configWrite() protected
Write a config variable
-
_setExtension() protected
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. The type with the highest weight will be set. If the highest weight has more than one type matching the extensions, the order in which extensions are specified determines which type will be set.
-
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. Unlike Cake\Network\Request::accepts() this method deals entirely with mapped content types.
-
addInputType() public deprecated
Add a new mapped input type. Mapped input types are automatically converted by RequestHandlerComponent during the startup() callback.
-
beforeRedirect() public
Handles (fakes) redirects for AJAX requests using requestAction()
-
beforeRender() public
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.
-
config() public
Usage
-
configShallow() public
Merge provided config with existing config. Unlike
config()
which does a recursive merge for nested keys, this method does a simple merge. -
convertXml() public
Helper method to parse xml input data, due to lack of anonymous functions this lives here.
-
implementedEvents() public
Events supported by this component.
-
initialize() public
Checks to see if a specific content type has been requested and sets RequestHandler::$ext accordingly. Checks the following in order: 1. The '_ext' value parsed by the Router. 2. A specific AJAX type request indicated by the presence of a header. 3. The Accept header. With the exception of an AJAX request indicated using the second header based method above, the type must have been configured in {@link Cake\Routing\Router}.
-
isAtom() public
Returns true if the current call accepts an Atom response, false otherwise
-
isMobile() public
Returns true if user agent string matches a mobile web browser, or if the client accepts WAP content.
-
isRss() public
Returns true if the current call accepts an RSS response, 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
-
log() public
Convenience method to write a message to Log. See Log::write() for more information on writing to logs.
-
mapAlias() public
Maps a content type alias back to its mime-type(s)
-
prefers() public
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.
-
renderAs() public
Sets either the view class if one exists or the layout and template path of the view. The names of these are derived from the $type input parameter.
-
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. This wraps several methods available on Cake\Network\Response. It also allows you to use Content-Type aliases.
-
responseType() public
Returns the current response type (Content-type header), or null if not alias exists
-
startup() public
The startup method of the RequestHandler enables several automatic behaviors related to the detection of certain properties of the HTTP request, including:
-
viewClassMap() public deprecated
Getter/setter for viewClassMap
Method Detail
__construct() ¶ public
__construct(Cake\Controller\ComponentRegistry $registry, array $config = [])
Constructor. Parses the accepted content types accepted by the client using HTTP_ACCEPT
Parameters
-
Cake\Controller\ComponentRegistry
$registry ComponentRegistry object.
-
array
$config optional Array of config.
__debugInfo() ¶ public
__debugInfo(): array
Returns an array that can be used to describe the internal state of this object.
Returns
array
__get() ¶ public
__get(string $name): mixed
Magic method for lazy loading $components.
Parameters
-
string
$name Name of component to get.
Returns
mixed
_configDelete() ¶ protected
_configDelete(string $key): void
Delete a single config key
Parameters
-
string
$key Key to delete.
Returns
void
Throws
Cake\Core\Exception\Exception
if attempting to clobber existing config
_configRead() ¶ protected
_configRead(string|null $key): mixed
Read a config variable
Parameters
-
string|null
$key Key to read.
Returns
mixed
_configWrite() ¶ protected
_configWrite(string|array $key, mixed $value, bool|string $merge = false): void
Write a config variable
Parameters
-
string|array
$key Key to write to.
-
mixed
$value Value to write.
-
bool|string
$merge optional True to merge recursively, 'shallow' for simple merge, false to overwrite, defaults to false.
Returns
void
Throws
Cake\Core\Exception\Exception
if attempting to clobber existing config
_setExtension() ¶ protected
_setExtension(Cake\Network\Request $request, Cake\Network\Response $response): void
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. The type with the highest weight will be set. If the highest weight has more than 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.
Parameters
-
Cake\Network\Request
$request The request instance.
-
Cake\Network\Response
$response The response instance.
Returns
void
accepts() ¶ public
accepts(string|array|null $type = null): mixed
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 Cake\Network\Request::accepts() this method deals entirely with mapped content types.
Usage:
$this->RequestHandler->accepts(['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
-
string|array|null
$type optional Can be null (or no parameter), a string type name, or an array of types
Returns
mixed
addInputType() ¶ public
addInputType(string $type, array $handler): void
Add a new mapped input type. Mapped input types are automatically converted by RequestHandlerComponent during the startup() callback.
Parameters
-
string
$type The type alias being converted, ie. json
-
array
$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.
Returns
void
Throws
Cake\Core\Exception\Exception
beforeRedirect() ¶ public
beforeRedirect(Cake\Event\Event $event, string|array $url, Cake\Network\Response $response): Cake\Network\Response|null
Handles (fakes) redirects for AJAX requests using requestAction()
Parameters
-
Cake\Event\Event
$event The Controller.beforeRedirect event.
-
string|array
$url A string or array containing the redirect location
-
Cake\Network\Response
$response The response object.
Returns
Cake\Network\Response|null
beforeRender() ¶ public
beforeRender(Cake\Event\Event $event): bool
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.
- If Router::extensions() is enabled, the layout and template type are
switched based on the parsed extension or
Accept
header. For example, ifcontroller/action.xml
is requested, the view path becomesapp/View/Controller/xml/action.ctp
. Also ifcontroller/action
is requested withAccept: application/xml
in the headers the view path will becomeapp/View/Controller/xml/action.ctp
. Layout and template types will only switch to mime-types recognized by Cake\Network\Response. If you need to declare additional mime-types, you can do so using Cake\Network\Response::type() in your controller's 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.
Parameters
-
Cake\Event\Event
$event The Controller.beforeRender event.
Returns
bool
config() ¶ public
config(string|array|null $key = null, mixed|null $value = null, bool $merge = true): mixed
Usage
Reading the whole config:
$this->config();
Reading a specific value:
$this->config('key');
Reading a nested value:
$this->config('some.nested.key');
Setting a specific value:
$this->config('key', $value);
Setting a nested value:
$this->config('some.nested.key', $value);
Updating multiple config settings at the same time:
$this->config(['one' => 'value', 'another' => 'value']);
Parameters
-
string|array|null
$key optional The key to get/set, or a complete array of configs.
-
mixed|null
$value optional The value to set.
-
bool
$merge optional Whether to recursively merge or overwrite existing config, defaults to true.
Returns
mixed
Throws
Cake\Core\Exception\Exception
When trying to set a key that is invalid.
configShallow() ¶ public
configShallow(string|array $key, mixed|null $value = null): $this
Merge provided config with existing config. Unlike config()
which does
a recursive merge for nested keys, this method does a simple merge.
Setting a specific value:
$this->config('key', $value);
Setting a nested value:
$this->config('some.nested.key', $value);
Updating multiple config settings at the same time:
$this->config(['one' => 'value', 'another' => 'value']);
Parameters
-
string|array
$key The key to set, or a complete array of configs.
-
mixed|null
$value optional The value to set.
Returns
$this
convertXml() ¶ public
convertXml(string $xml): array
Helper method to parse xml input data, due to lack of anonymous functions this lives here.
Parameters
-
string
$xml XML string.
Returns
array
implementedEvents() ¶ public
implementedEvents(): array
Events supported by this component.
Uses Conventions to map controller events to standard component callback method names. By defining one of the callback methods a component is assumed to be interested in the related event.
Override this method if you need to add non-conventional event listeners. Or if you want components to listen to non-standard events.
Returns
array
initialize() ¶ public
initialize(array $config): void
Checks to see if a specific content type has been requested and sets RequestHandler::$ext accordingly. Checks the following in order: 1. The '_ext' value parsed by the Router. 2. A specific AJAX type request indicated by the presence of a header. 3. The Accept header. With the exception of an AJAX request indicated using the second header based method above, the type must have been configured in {@link Cake\Routing\Router}.
Implement this method to avoid having to overwrite the constructor and call parent.
Parameters
-
array
$config The config data.
Returns
void
See Also
isAtom() ¶ public
isAtom(): bool
Returns true if the current call accepts an Atom response, false otherwise
Returns
bool
isMobile() ¶ public
isMobile(): bool
Returns true if user agent string matches a mobile web browser, or if the client accepts WAP content.
Returns
bool
isRss() ¶ public
isRss(): bool
Returns true if the current call accepts an RSS response, false otherwise
Returns
bool
isXml() ¶ public
isXml(): bool
Returns true if the current call accepts an XML response, false otherwise
Returns
bool
log() ¶ public
log(mixed $msg, int|string $level = LogLevel::ERROR, string|array $context = []): bool
Convenience method to write a message to Log. See Log::write() for more information on writing to logs.
Parameters
-
mixed
$msg Log message.
-
int|string
$level optional Error level.
-
string|array
$context optional Additional log data relevant to this message.
Returns
bool
mapAlias() ¶ public
mapAlias(string|array $alias): string|null|array
Maps a content type alias back to its mime-type(s)
Parameters
-
string|array
$alias String alias to convert back into a content type. Or an array of aliases to map.
Returns
string|null|array
prefers() ¶ public
prefers(string|array|null $type = null): mixed
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.
Parameters
-
string|array|null
$type optional An optional array of 'friendly' content-type names, i.e. 'html', 'xml', 'js', etc.
Returns
mixed
renderAs() ¶ public
renderAs(Cake\Controller\Controller $controller, string $type, array $options = []): void
Sets either the view class if one exists or the layout and template path of the view. The names of these are derived from the $type input parameter.
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', ['attachment' => 'myfile.xml'];
Parameters
-
Cake\Controller\Controller
$controller A reference to a controller object
-
string
$type Type of response to send (e.g: 'ajax')
-
array
$options optional Array of options to use
Returns
void
See Also
requestedWith() ¶ public
requestedWith(string|array|null $type = null): mixed
Determines the content type of the data the client has sent (i.e. in a POST request)
Parameters
-
string|array|null
$type optional Can be null (or no parameter), a string type name, or an array of types
Returns
mixed
respondAs() ¶ public
respondAs(string|array $type, array $options = []): bool
Sets the response header based on type map index name. This wraps several methods available on Cake\Network\Response. It also allows you to use Content-Type aliases.
Parameters
-
string|array
$type Friendly type name, i.e. 'html' or 'xml', or a full content-type, like 'application/x-shockwave'.
-
array
$options optional 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
bool
responseType() ¶ public
responseType(): mixed
Returns the current response type (Content-type header), or null if not alias exists
Returns
mixed
startup() ¶ public
startup(Cake\Event\Event $event): void
The startup method of the RequestHandler enables several automatic behaviors related to the detection of certain properties of the HTTP request, including:
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
-
Cake\Event\Event
$event The startup event that was fired.
Returns
void
viewClassMap() ¶ public
viewClassMap(array|string|null $type = null, array|null $viewClass = null): array|string
Getter/setter for viewClassMap
Parameters
-
array|string|null
$type optional The type string or array with format
['type' => 'viewClass']
to map one or more-
array|null
$viewClass optional The viewClass to be used for the type without
View
appended
Returns
array|string
Property Detail
$_configInitialized ¶ protected
Whether the config property has already been configured with defaults
Type
bool
$_defaultConfig ¶ protected
Default config
These are merged with user-provided config when the component is used.
checkHttpCache
- Whether to check for HTTP cache.viewClassMap
- Mapping between type and view classes. If undefined json, xml, and ajax will be mapped. Defining any types will omit the defaults.inputTypeMap
- A mapping between types and deserializers for request bodies. If undefined json & xml will be mapped. Defining any types will omit the defaults.
Type
array
$_registry ¶ protected
Component registry class used to lazy load components.
Type
Cake\Controller\ComponentRegistry