FlashComponent
extends Component
in package
The CakePHP FlashComponent provides a way for you to write a flash variable to the session from your controllers, to be rendered in a view with the FlashHelper.
Table of Contents
Properties
- $_componentMap : array<string, array<string|int, mixed>>
- A component lookup table used to lazy load component objects.
- $_config : array<string, mixed>
- Runtime config
- $_configInitialized : bool
- Whether the config property has already been configured with defaults
- $_defaultConfig : array<string, mixed>
- Default configuration
- $_registry : ComponentRegistry
- Component registry class used to lazy load components.
- $components : array<string|int, mixed>
- Other Components this component uses.
Methods
- __call() : void
- Magic method for verbose flash methods based on element names.
- __construct() : mixed
- Constructor
- __debugInfo() : array<string, mixed>
- Returns an array that can be used to describe the internal state of this object.
- __get() : Component|null
- Magic method for lazy loading $components.
- configShallow() : $this
- Proxy method to FlashMessage instance.
- error() : void
- getConfig() : mixed
- Proxy method to FlashMessage instance.
- getConfigOrFail() : mixed
- Proxy method to FlashMessage instance.
- getController() : Controller
- Get the controller this component is bound to.
- implementedEvents() : array<string, mixed>
- Get the Controller callbacks this Component is interested in.
- info() : void
- initialize() : void
- Constructor hook method.
- log() : bool
- Convenience method to write a message to Log. See Log::write() for more information on writing to logs.
- set() : void
- Used to set a session variable that can be used to output messages in the view.
- setConfig() : $this
- Proxy method to FlashMessage instance.
- success() : void
- warning() : void
- _configDelete() : void
- Deletes a single config key.
- _configRead() : mixed
- Reads a config key.
- _configWrite() : void
- Writes a config key.
- flash() : FlashMessage
- Get flash message utility instance.
Properties
$_componentMap
A component lookup table used to lazy load component objects.
protected
array<string, array<string|int, mixed>>
$_componentMap
= []
$_config
Runtime config
protected
array<string, mixed>
$_config
= []
$_configInitialized
Whether the config property has already been configured with defaults
protected
bool
$_configInitialized
= false
$_defaultConfig
Default configuration
protected
array<string, mixed>
$_defaultConfig
= ['key' => 'flash', 'element' => 'default', 'params' => [], 'clear' => false, 'duplicate' => true]
$_registry
Component registry class used to lazy load components.
protected
ComponentRegistry
$_registry
$components
Other Components this component uses.
protected
array<string|int, mixed>
$components
= []
Methods
__call()
Magic method for verbose flash methods based on element names.
public
__call(string $name, array<string|int, mixed> $args) : void
For example: $this->Flash->success('My message') would use the
success.php
element under templates/element/flash/
for rendering the
flash message.
If you make consecutive calls to this method, the messages will stack (if they are set with the same flash key)
Note that the parameter element
will be always overridden. In order to call a
specific element from a plugin, you should set the plugin
option in $args.
For example: $this->Flash->warning('My message', ['plugin' => 'PluginName'])
would
use the warning.php
element under plugins/PluginName/templates/element/flash/
for
rendering the flash message.
Parameters
- $name : string
-
Element name to use.
- $args : array<string|int, mixed>
-
Parameters to pass when calling
FlashComponent::set()
.
Tags
__construct()
Constructor
public
__construct(ComponentRegistry $registry[, array<string, mixed> $config = [] ]) : mixed
Parameters
- $registry : ComponentRegistry
-
A component registry this component can use to lazy load its components.
- $config : array<string, mixed> = []
-
Array of configuration settings.
__debugInfo()
Returns an array that can be used to describe the internal state of this object.
public
__debugInfo() : array<string, mixed>
Return values
array<string, mixed>__get()
Magic method for lazy loading $components.
public
__get(string $name) : Component|null
Parameters
- $name : string
-
Name of component to get.
Return values
Component|null —A Component object or null.
configShallow()
Proxy method to FlashMessage instance.
public
configShallow(array<string, mixed>|string $key[, mixed|null $value = null ]) : $this
Parameters
- $key : array<string, mixed>|string
-
The key to set, or a complete array of configs.
- $value : mixed|null = null
-
The value to set.
Return values
$thiserror()
public
error([string $message = ][, array<string|int, mixed> $options = [] ]) : void
Set a message using "error" element
Parameters
- $message : string =
- $options : array<string|int, mixed> = []
getConfig()
Proxy method to FlashMessage instance.
public
getConfig([string|null $key = null ][, mixed $default = null ]) : mixed
Parameters
- $key : string|null = null
-
The key to get or null for the whole config.
- $default : mixed = null
-
The return value when the key does not exist.
Return values
mixed —Configuration data at the named key or null if the key does not exist.
getConfigOrFail()
Proxy method to FlashMessage instance.
public
getConfigOrFail(string $key) : mixed
Parameters
- $key : string
-
The key to get.
Tags
Return values
mixed —Configuration data at the named key
getController()
Get the controller this component is bound to.
public
getController() : Controller
Return values
Controller —The bound controller.
implementedEvents()
Get the Controller callbacks this Component is interested in.
public
implementedEvents() : array<string, mixed>
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.
Return values
array<string, mixed>info()
public
info([string $message = ][, array<string|int, mixed> $options = [] ]) : void
Set a message using "info" element
Parameters
- $message : string =
- $options : array<string|int, mixed> = []
initialize()
Constructor hook method.
public
initialize(array<string, mixed> $config) : void
Implement this method to avoid having to overwrite the constructor and call parent.
Parameters
- $config : array<string, mixed>
-
The configuration settings provided to this component.
log()
Convenience method to write a message to Log. See Log::write() for more information on writing to logs.
public
log(string $message[, string|int $level = LogLevel::ERROR ][, array<string|int, mixed>|string $context = [] ]) : bool
Parameters
- $message : string
-
Log message.
- $level : string|int = LogLevel::ERROR
-
Error level.
- $context : array<string|int, mixed>|string = []
-
Additional log data relevant to this message.
Return values
bool —Success of log write.
set()
Used to set a session variable that can be used to output messages in the view.
public
set(Throwable|string $message[, array<string, mixed> $options = [] ]) : void
If you make consecutive calls to this method, the messages will stack (if they are set with the same flash key)
In your controller: $this->Flash->set('This has been saved');
Options:
-
key
The key to set under the session's Flash key -
element
The element used to render the flash message. Default to 'default'. -
params
An array of variables to make available when using an element -
clear
A bool stating if the current stack should be cleared to start a new one -
escape
Set to false to allow templates to print out HTML content
Parameters
- $message : Throwable|string
-
Message to be flashed. If an instance of \Throwable the throwable message will be used and code will be set in params.
- $options : array<string, mixed> = []
-
An array of options
setConfig()
Proxy method to FlashMessage instance.
public
setConfig(array<string, mixed>|string $key[, mixed|null $value = null ][, bool $merge = true ]) : $this
Parameters
- $key : array<string, mixed>|string
-
The key to set, or a complete array of configs.
- $value : mixed|null = null
-
The value to set.
- $merge : bool = true
-
Whether to recursively merge or overwrite existing config, defaults to true.
Tags
Return values
$thissuccess()
public
success([string $message = ][, array<string|int, mixed> $options = [] ]) : void
Set a message using "success" element
Parameters
- $message : string =
- $options : array<string|int, mixed> = []
warning()
public
warning([string $message = ][, array<string|int, mixed> $options = [] ]) : void
Set a message using "warning" element
Parameters
- $message : string =
- $options : array<string|int, mixed> = []
_configDelete()
Deletes a single config key.
protected
_configDelete(string $key) : void
Parameters
- $key : string
-
Key to delete.
Tags
_configRead()
Reads a config key.
protected
_configRead(string|null $key) : mixed
Parameters
- $key : string|null
-
Key to read.
_configWrite()
Writes a config key.
protected
_configWrite(array<string, mixed>|string $key, mixed $value[, string|bool $merge = false ]) : void
Parameters
- $key : array<string, mixed>|string
-
Key to write to.
- $value : mixed
-
Value to write.
- $merge : string|bool = false
-
True to merge recursively, 'shallow' for simple merge, false to overwrite, defaults to false.
Tags
flash()
Get flash message utility instance.
protected
flash() : FlashMessage