Flash
in package
A class that offers a simple interface to write flash messages.
Example:
// write a flash message
$flash = new Flash();
$flash->message('Some message!');
// write a flash message that will be rendered immediately
$flash->message('Some other message!', 'success', true);
// write a flash message with a predefined type
$flash->error('Some error!', true);
// write a flash message with your own type
$flash->yourCustomType('Custom type!');
// render the flash messages
$flash->render();
// render the flash messages using a custom callback
$flash->render(function ($text, $type) {
return sprintf('<div class="alert alert-%s">%s</div>', $type, $text);
});
Tags
Table of Contents
- $messages : array<string|int, mixed>
- $name : string
- __call() : mixed
- Aliases some magic methods for `self::message()`.
- __construct() : mixed
- Class constructor.
- __invoke() : mixed
- Makes the class callable as a function, the call is forwarded to `self::message()`.
- __toString() : mixed
- Returns the HTML containing the flash messages.
- anyType() :
- error() :
- info() :
- message() : $this
- Writes a flash message to the session.
- render() : string
- Renders the flash messages using the default callback or the passed one.
- success() :
- warning() :
Properties
$messages
private
array<string|int, mixed>
$messages
$name
private
string
$name
Methods
__call()
Aliases some magic methods for `self::message()`.
public
__call(string $method, array<string|int, mixed> $arguments) : mixed
Parameters
- $method : string
- $arguments : array<string|int, mixed>
Return values
mixed —__construct()
Class constructor.
public
__construct([string|null $name = null ]) : mixed
Parameters
- $name : string|null = null
-
The name of the flash messages store (session key).
Return values
mixed —__invoke()
Makes the class callable as a function, the call is forwarded to `self::message()`.
public
__invoke() : mixed
Return values
mixed —__toString()
Returns the HTML containing the flash messages.
public
__toString() : mixed
Return values
mixed —anyType()
public
static anyType(string $text, bool $now = false) :
Adds a message of anyType
to the flash. anyType
is user defined and will be used as a CSS class if the default rendering callback is used (camelCase
will be changed to kebab-case
automatically).
Parameters
- $text : string
- $now = false : bool
Return values
—error()
public
static error(string $text, bool $now = false) :
Adds an error
message to the flash.
Parameters
- $text : string
- $now = false : bool
Return values
—info()
public
static info(string $text, bool $now = false) :
Adds an info
message to the flash.
Parameters
- $text : string
- $now = false : bool
Return values
—message()
Writes a flash message to the session.
public
message(string $type, string $text[, bool $now = false ]) : $this
Parameters
- $type : string
-
Message type.
- $text : string
-
Message text.
- $now : bool = false
-
[optional] Whether to write and make the message available for rendering immediately or wait for the next request.
Return values
$this —render()
Renders the flash messages using the default callback or the passed one.
public
render([callable|null $callback = null ]) : string
This method will be called automatically if the object is casted to a string.
Parameters
- $callback : callable|null = null
-
Rendering callback. The callback will be passed:
$text
,$type
Return values
string —The result of the passed callback or the default one.
success()
public
static success(string $text, bool $now = false) :
Adds a success
message to the flash.
Parameters
- $text : string
- $now = false : bool
Return values
—warning()
public
static warning(string $text, bool $now = false) :
Adds a warning
message to the flash.
Parameters
- $text : string
- $now = false : bool