Base class for an individual Component. Components provide reusable bits of controller logic that can be composed into a controller. Components also provide request life-cycle callbacks for injecting logic at specific points.
Like Controller and Table, this class has an initialize() hook that you can use to add custom 'constructor' logic. It is important to remember that each request (and sub-request) will only make one instance of any given component.
Components can provide several callbacks that are fired at various stages of the request cycle. The available callbacks are:
beforeFilter(EventInterface $event) Called before Controller::beforeFilter() method by default.startup(EventInterface $event) Called after Controller::beforeFilter() method, and before the controller action is called.beforeRender(EventInterface $event) Called before Controller::beforeRender(), and before the view class is loaded.afterFilter(EventInterface $event) Called after the action is complete and the view has been rendered but before Controller::afterFilter().beforeRedirect(EventInterface $event $url, Response $response) Called before a redirect is done. Allows you to change the URL that will be redirected to by returning a Response instance with new URL set using Response::location(). Redirection can be prevented by stopping the event propagation.While the controller is not an explicit argument for the callback methods it is the subject of each event and can be fetched using EventInterface::getSubject().
array<string, array>A component lookup table used to lazy load component objects.
array<string, mixed>Runtime config
boolWhether the config property has already been configured with defaults
array<string, mixed>Default config
Cake\Controller\ComponentRegistryComponent registry class used to lazy load components.
arrayOther Components this component uses.
Constructor
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.
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.
Get the Controller callbacks this Component is interested in.
Constructor hook method.
Convenience method to write a message to Log. See Log::write() for more information on writing to logs.
Sets the config.
__construct(Cake\Controller\ComponentRegistry $registry, array<string, mixed> $config = [])
Constructor
Cake\Controller\ComponentRegistry $registry A component registry this component can use to lazy load its components.
array<string, mixed> $config optional Array of configuration settings.
__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.
voidCake\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.
voidCake\Core\Exception\CakeExceptionconfigShallow(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.
$thisgetConfig(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.
mixedgetConfigOrFail(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.
mixedInvalidArgumentExceptiongetController(): Cake\Controller\Controller
Get the controller this component is bound to.
Cake\Controller\ControllerimplementedEvents(): array<string, mixed>
Get the Controller callbacks this Component is interested in.
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.
voidlog(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.
boolsetConfig(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.
$thisCake\Core\Exception\CakeExceptionA 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
boolDefault config
These are merged with user-provided config when the component is used.
array<string, mixed>Component registry class used to lazy load components.
Cake\Controller\ComponentRegistryOther Components this component uses.
array
© 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.html