-
Notifications
You must be signed in to change notification settings - Fork 2
Flash
The Flash class is a session-based flash data manager. It stores temporary data — validation errors, status messages, and old form input — that persists for exactly one subsequent request and is then automatically discarded.
This pattern is most commonly used before a redirect: you flash the data, redirect the user, and read it back on the next page.
Requires: PHP 8.0+
use Webrium\Flash;
// Before redirecting — store the data
Flash::withError(['email' => 'Invalid email address.'])
->withInput();
// redirect(...)
// On the next page — read the data
echo Flash::error('email'); // Invalid email address.
echo Flash::old('email'); // user@example.comStore validation errors before a redirect. Accepts a single string or an associative field => message array.
Single message:
Flash::withError('Something went wrong. Please try again.');Multiple field errors:
Flash::withError([
'email' => 'A valid email address is required.',
'password' => 'Password must be at least 8 characters.',
]);Check if any errors exist:
if (Flash::hasErrors()) {
// show error summary
}Check a specific field:
if (Flash::hasError('email')) {
echo Flash::error('email');
}Get a single field error:
echo Flash::error('email') ?? '';Get all errors:
$errors = Flash::errors(); // ['email' => '...', 'password' => '...']
foreach ($errors as $field => $message) {
echo "<p>{$field}: {$message}</p>";
}In a form (Blade-style example):
<input type="email" name="email" value="<?= Flash::old('email') ?>">
<?php if (Flash::hasError('email')): ?>
<span class="error"><?= Flash::error('email') ?></span>
<?php endif; ?>Manually remove all flashed errors from the session:
Flash::clearErrors();Three message types are available, each rendered using its own configurable template.
Success:
Flash::success('Your profile has been updated.');Error:
Flash::errorMessage('Failed to process your request.');Normal / neutral:
Flash::message('Maintenance is scheduled for tonight at 11 PM.');Check if a message is waiting:
if (Flash::hasMessage()) {
echo Flash::getMessage();
}Render using the configured template (default):
echo Flash::getMessage();Get the raw text only, without any template:
$text = Flash::getMessage(raw: true);getMessage() consumes the message from the session — it will not be available on the next call or next request.
Define PHP constants to control how each message type is rendered. Use @text as the placeholder for the message content.
define('MESSAGE_SCRIPT', '<div class="alert alert-info">@text</div>');
define('MESSAGE_SCRIPT_SUCCESS', '<div class="alert alert-success">@text</div>');
define('MESSAGE_SCRIPT_ERROR', '<div class="alert alert-danger">@text</div>');If no constant is defined for a given type, it falls back to:
<script>alert('@text');</script>Note: Message text is always escaped with
htmlspecialcharsbefore being inserted into the template to prevent XSS.
Flash::clearMessage();Repopulate form fields after a failed submission by flashing the current request input before redirecting.
Call withInput() before the redirect. It reads all current request input using the global input() helper.
Flash::withInput();Single field with optional default:
echo Flash::old('username');
echo Flash::old('country', 'NL'); // fallback defaultAll old input at once:
$old = Flash::oldAll(); // ['username' => '...', 'email' => '...']In a form:
<input type="text" name="name" value="<?= Flash::old('name') ?>">
<input type="email" name="email" value="<?= Flash::old('email') ?>">All write methods (withError, success, errorMessage, message, withInput) return a static instance, so they can be chained freely.
Flash::withError(['email' => 'Invalid email.'])
->withInput()
->success('Some other message.'); // contrived, but validA typical controller pattern:
public function store(Request $request)
{
$validated = $request->validate([...]);
if (!$validated) {
Flash::withError($errors)->withInput();
return redirect()->back();
}
// ... save record ...
Flash::success('Record created successfully.');
return redirect('/dashboard');
}| Method | Signature | Description |
|---|---|---|
withError |
withError(string|array $errors): static |
Flash one or more errors to the session |
hasErrors |
hasErrors(): bool |
True if any errors are stored |
hasError |
hasError(string $field): bool |
True if an error exists for the given field |
error |
error(string $field): ?string |
Get the error message for a field, or null |
errors |
errors(): array |
Get all errors as an associative array |
clearErrors |
clearErrors(): void |
Remove all errors from the session |
| Method | Signature | Description |
|---|---|---|
success |
success(string $text): static |
Flash a success message |
errorMessage |
errorMessage(string $text): static |
Flash an error message |
message |
message(string $text): static |
Flash a neutral message |
hasMessage |
hasMessage(): bool |
True if a message is waiting |
getMessage |
getMessage(bool $raw = false): string|false |
Retrieve and render (or return raw text of) the message |
clearMessage |
clearMessage(): void |
Remove the message from the session |
| Method | Signature | Description |
|---|---|---|
withInput |
withInput(): static |
Flash the current request's input to the session |
old |
old(string $field, mixed $default = null): mixed |
Get a single old input value |
oldAll |
oldAll(): array |
Get all old input as an associative array |