Request object handling for alternative HTTP requests.
This Component checks for requests for different content types like JSON, XML, XMLHttpRequest(AJAX) and configures the response object and view builder accordingly.
It can also check for HTTP caching headers like Last-Modified
, If-Modified-Since
etc. and return a response accordingly.
array<string, array>
A component lookup table used to lazy load component objects.
array<string, mixed>
Runtime config
bool
Whether the config property has already been configured with defaults
array<string, mixed>
Default config
Cake\Controller\ComponentRegistry
Component registry class used to lazy load components.
string|null
The template type to use when rendering the given content type.
array
Other Components this component uses.
string|null
Contains the file extension parsed out by the Router
Constructor. Parses the accepted content types accepted by the client using HTTP_ACCEPT
Returns an array that can be used to describe the internal state of this object.
Magic method for lazy loading $components.
Deletes a single config key.
Reads a config key.
Writes a config key.
Set the extension based on the Accept
header or URL extension.
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 {@link \Cake\Http\ServerRequest::accepts()} this method deals entirely with mapped content types.
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.
Merge provided config with existing config. Unlike config()
which does a recursive merge for nested keys, this method does a simple merge.
Returns the config.
Returns the config for this specific key.
Get the controller this component is bound to.
Events supported by this component.
Constructor hook method.
Convenience method to write a message to Log. See Log::write() for more information on writing to logs.
Maps a content type alias back to its mime-type(s)
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 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.
Determines the content type of the data the client has sent (i.e. in a POST request)
Sets the response header based on type map index name. This wraps several methods available on {@link \Cake\Http\Response}. It also allows you to use Content-Type aliases.
Sets the config.
The startup method of the RequestHandler enables several automatic behaviors related to the detection of certain properties of the HTTP request, including:
__construct(Cake\Controller\ComponentRegistry $registry, array<string, mixed> $config = [])
Constructor. Parses the accepted content types accepted by the client using HTTP_ACCEPT
Cake\Controller\ComponentRegistry
$registry ComponentRegistry object.
array<string, mixed>
$config optional Array of config.
__debugInfo(): array<string, mixed>
Returns an array that can be used to describe the internal state of this object.
array<string, mixed>
__get(string $name): Cake\Controller\Component|null
Magic method for lazy loading $components.
string
$name Name of component to get.
Cake\Controller\Component|null
_configDelete(string $key): void
Deletes a single config key.
string
$key Key to delete.
void
Cake\Core\Exception\CakeException
_configRead(string|null $key): mixed
Reads a config key.
string|null
$key Key to read.
mixed
_configWrite(array<string, mixed>|string $key, mixed $value, string|bool $merge = false): void
Writes a config key.
array<string, mixed>|string
$key Key to write to.
mixed
$value Value to write.
string|bool
$merge optional True to merge recursively, 'shallow' for simple merge, false to overwrite, defaults to false.
void
Cake\Core\Exception\CakeException
_setExtension(Cake\Http\ServerRequest $request, Cake\Http\Response $response): void
Set the extension based on the Accept
header or URL extension.
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.
Cake\Http\ServerRequest
$request The request instance.
Cake\Http\Response
$response The response instance.
void
accepts(array<string>|string|null $type = null): array|bool|string|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 {@link \Cake\Http\ServerRequest::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.
array<string>|string|null
$type optional Can be null (or no parameter), a string type name, or an array of types
array|bool|string|null
beforeRender(Cake\Event\EventInterface $event): void
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.
Accept
header. For example, if controller/action.xml
is requested, the view path becomes templates/Controller/xml/action.php
. Also, if controller/action
is requested with Accept: application/xml
in the headers the view path will become templates/Controller/xml/action.php
. Layout and template types will only switch to mime-types recognized by \Cake\Http\Response. If you need to declare additional mime-types, you can do so using {@link \Cake\Http\Response::setTypeMap()} in your controller's beforeFilter() method.Cake\Event\EventInterface
$event The Controller.beforeRender event.
void
Cake\Http\Exception\NotFoundException
configShallow(array<string, mixed>|string $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->configShallow('key', $value);
Setting a nested value:
$this->configShallow('some.nested.key', $value);
Updating multiple config settings at the same time:
$this->configShallow(['one' => 'value', 'another' => 'value']);
array<string, mixed>|string
$key The key to set, or a complete array of configs.
mixed|null
$value optional The value to set.
$this
getConfig(string|null $key = null, mixed $default = null): mixed
Returns the config.
Reading the whole config:
$this->getConfig();
Reading a specific value:
$this->getConfig('key');
Reading a nested value:
$this->getConfig('some.nested.key');
Reading with default value:
$this->getConfig('some-key', 'default-value');
string|null
$key optional The key to get or null for the whole config.
mixed
$default optional The return value when the key does not exist.
mixed
getConfigOrFail(string $key): mixed
Returns the config for this specific key.
The config value for this key must exist, it can never be null.
string
$key The key to get.
mixed
InvalidArgumentException
getController(): Cake\Controller\Controller
Get the controller this component is bound to.
Cake\Controller\Controller
implementedEvents(): array<string, mixed>
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.
array<string, mixed>
initialize(array<string, mixed> $config): void
Constructor hook method.
Implement this method to avoid having to overwrite the constructor and call parent.
array<string, mixed>
$config The configuration settings provided to this component.
void
log(string $message, string|int $level = LogLevel::ERROR, array|string $context = []): bool
Convenience method to write a message to Log. See Log::write() for more information on writing to logs.
string
$message Log message.
string|int
$level optional Error level.
array|string
$context optional Additional log data relevant to this message.
bool
mapAlias(array|string $alias): array|string|null
Maps a content type alias back to its mime-type(s)
array|string
$alias String alias to convert back into a content type. Or an array of aliases to map.
array|string|null
prefers(array<string>|string|null $type = null): string|bool|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.
array<string>|string|null
$type optional An optional array of 'friendly' content-type names, i.e. 'html', 'xml', 'js', etc.
string|bool|null
renderAs(Cake\Controller\Controller $controller, string $type, array<string, mixed> $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.
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'];
Cake\Controller\Controller
$controller A reference to a controller object
string
$type Type of response to send (e.g: 'ajax')
array<string, mixed>
$options optional Array of options to use
void
requestedWith(array<string>|string|null $type = null): mixed
Determines the content type of the data the client has sent (i.e. in a POST request)
array<string>|string|null
$type optional Can be null (or no parameter), a string type name, or an array of types
mixed
respondAs(string $type, array<string, mixed> $options = []): bool
Sets the response header based on type map index name. This wraps several methods available on {@link \Cake\Http\Response}. It also allows you to use Content-Type aliases.
string
$type Friendly type name, i.e. 'html' or 'xml', or a full content-type, like 'application/x-shockwave'.
array<string, mixed>
$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.
bool
setConfig(array<string, mixed>|string $key, mixed|null $value = null, bool $merge = true): $this
Sets the config.
Setting a specific value:
$this->setConfig('key', $value);
Setting a nested value:
$this->setConfig('some.nested.key', $value);
Updating multiple config settings at the same time:
$this->setConfig(['one' => 'value', 'another' => 'value']);
array<string, mixed>|string
$key The key to 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.
$this
Cake\Core\Exception\CakeException
startup(Cake\Event\EventInterface $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.
Cake\Event\EventInterface
$event The startup event that was fired.
void
A component lookup table used to lazy load component objects.
array<string, array>
Runtime config
array<string, mixed>
Whether the config property has already been configured with defaults
bool
Default config
These are merged with user-provided config when the component is used.
checkHttpCache
- Whether to check for HTTP cache. Default true
.viewClassMap
- Mapping between type and view classes. If undefined JSON, XML, and AJAX will be mapped. Defining any types will omit the defaults.array<string, mixed>
Component registry class used to lazy load components.
Cake\Controller\ComponentRegistry
The template type to use when rendering the given content type.
string|null
Other Components this component uses.
array
Contains the file extension parsed out by the Router
string|null
© 2005–present 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/4.4/class-Cake.Controller.Component.RequestHandlerComponent.html