Fr3nch13/CakePHP Utilities

FlashMessage
in package
uses InstanceConfigTrait

The FlashMessage class provides a way for you to write a flash variable to the session, to be rendered in a view with the FlashHelper.

Table of Contents

Properties

$_config  : array<string, mixed>
Runtime config
$_configInitialized  : bool
Whether the config property has already been configured with defaults
$_defaultConfig  : array<string, mixed>
Default configuration
$session  : Session

Methods

__construct()  : mixed
Constructor
configShallow()  : $this
Merge provided config with existing config. Unlike `config()` which does a recursive merge for nested keys, this method does a simple merge.
consume()  : array<string|int, mixed>|null
Get the messages for given key and remove from session.
error()  : void
Set an success message.
getConfig()  : mixed
Returns the config.
getConfigOrFail()  : mixed
Returns the config for this specific key.
info()  : void
Set an info message.
set()  : void
Store flash messages that can be output in the view.
setConfig()  : $this
Sets the config.
setExceptionMessage()  : void
Set an exception's message as flash message.
success()  : void
Set a success message.
warning()  : void
Set a warning message.
_configDelete()  : void
Deletes a single config key.
_configRead()  : mixed
Reads a config key.
_configWrite()  : void
Writes a config key.

Properties

$_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', 'plugin' => null, 'params' => [], 'clear' => false, 'duplicate' => true]

Methods

__construct()

Constructor

public __construct(Session $session[, array<string, mixed> $config = [] ]) : mixed
Parameters
$session : Session

Session instance.

$config : array<string, mixed> = []

Config array.

Tags
see
FlashMessage::set()

For list of valid config keys.

configShallow()

Merge provided config with existing config. Unlike `config()` which does a recursive merge for nested keys, this method does a simple merge.

public configShallow(array<string, mixed>|string $key[, mixed|null $value = null ]) : $this

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']);
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
$this

consume()

Get the messages for given key and remove from session.

public consume(string $key) : array<string|int, mixed>|null
Parameters
$key : string

The key for get messages for.

Return values
array<string|int, mixed>|null

error()

Set an success message.

public error(string $message[, array<string, mixed> $options = [] ]) : void

The 'element' option will be set to 'error'.

Parameters
$message : string

Message to flash.

$options : array<string, mixed> = []

An array of options.

Tags
see
FlashMessage::set()

For list of valid options

getConfig()

Returns the config.

public getConfig([string|null $key = null ][, mixed $default = null ]) : mixed

Usage

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');
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()

Returns the config for this specific key.

public getConfigOrFail(string $key) : mixed

The config value for this key must exist, it can never be null.

Parameters
$key : string

The key to get.

Tags
throws
InvalidArgumentException
Return values
mixed

Configuration data at the named key

info()

Set an info message.

public info(string $message[, array<string, mixed> $options = [] ]) : void

The 'element' option will be set to 'info'.

Parameters
$message : string

Message to flash.

$options : array<string, mixed> = []

An array of options.

Tags
see
FlashMessage::set()

For list of valid options

set()

Store flash messages that can be output in the view.

public set(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)

Options:

  • key The key to set under the session's Flash key.
  • element The element used to render the flash message. You can use 'SomePlugin.name' style value for flash elements from a plugin.
  • plugin Plugin name to use element from.
  • params An array of variables to be made available to the 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 : string

Message to be flashed.

$options : array<string, mixed> = []

An array of options

Tags
see
FlashMessage::$_defaultConfig

For default values for the options.

setConfig()

Sets the config.

public setConfig(array<string, mixed>|string $key[, mixed|null $value = null ][, bool $merge = true ]) : $this

Usage

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']);
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
throws
CakeException

When trying to set a key that is invalid.

Return values
$this

setExceptionMessage()

Set an exception's message as flash message.

public setExceptionMessage(Throwable $exception[, array<string, mixed> $options = [] ]) : void

The following options will be set by default if unset:

'element' => 'error',
`params' => ['code' => $exception->getCode()]
Parameters
$exception : Throwable

Exception instance.

$options : array<string, mixed> = []

An array of options.

Tags
see
FlashMessage::set()

For list of valid options

success()

Set a success message.

public success(string $message[, array<string, mixed> $options = [] ]) : void

The 'element' option will be set to 'success'.

Parameters
$message : string

Message to flash.

$options : array<string, mixed> = []

An array of options.

Tags
see
FlashMessage::set()

For list of valid options

warning()

Set a warning message.

public warning(string $message[, array<string, mixed> $options = [] ]) : void

The 'element' option will be set to 'warning'.

Parameters
$message : string

Message to flash.

$options : array<string, mixed> = []

An array of options.

Tags
see
FlashMessage::set()

For list of valid options

_configDelete()

Deletes a single config key.

protected _configDelete(string $key) : void
Parameters
$key : string

Key to delete.

Tags
throws
CakeException

if attempting to clobber existing config

_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
throws
CakeException

if attempting to clobber existing config


        
On this page

Search results