One redirect notice in Laravel PHP
Scope
Inline one plain-text notification after a form action using native Laravel session flash and an escaped application layout. Exclude raw HTML bodies, multiple-message queues, overlays, important/dismiss UI, macros and JS/toast APIs. Upstream's Composer manifest declares MIT; retain applicable notices for adapted source. Check get-recipe's current port catalog when considering alternatives. This guidance has not been exercised in your app; verify it below.
Source behaviour
src/Laracasts/Flash/FlashNotifier.php accumulates Message objects and flashes flash_notification. error maps to danger; a falsy message changes the last object instead of adding one. Message.php::update filters falsy attributes. LaravelSessionStore.php delegates to the framework flash operation. src/views/message.blade.php renders message bodies unescaped, then forgets the key. tests/FlashTest.php covers levels/queues/overlays and store interactions, not real redirects/session aging.
This extraction deliberately uses a fresh app_notice key, one message with last-write-wins behavior, escaped text, empty-text rejection and acceptance of string "0". It does not preserve the old raw-HTML/fluent/falsy contract. Keep those callers separate until explicitly adapted.
Implementation
- Inventory producers, helpers, views, redirect chains and session middleware. Capture the chosen action's response/notice behavior; do not remove a provider while other queue/modal callers need it.
- Authorize and validate/CSRF-protect the action. Commit persistence before flashing success. Use a trusted redirect route, not arbitrary request URLs. Keep secrets/exception internals out of notices; session content is not authorization.
- Native
session()->flash('app_notice', $payload)stores the message for current/next request. Use a literal new key to avoid mixing old Message objects with arrays. Do not use ordinaryput, unconditional reflash or a process-global notifier in a long-lived worker. - At one destination layout/controller boundary consume
session()->pull('app_notice'), validate its array/string/level shape, and render text with escaped Blade interpolation ({{ ... }}), never raw output. Map only fixed allowed levels to CSS classes. Do not escape in the constructor and then escape again in the view. - Preserve the existing driver's session persistence/error policy and secure cookie configuration. Extra requests/redirects/tabs may age or consume the flash before a person sees it. Retain existing session blocking if needed; native flash is not an exactly-once notification transport. A queued job is not a request-session producer.
Complete pure payload constructor with namespaced-safe exceptions. The action should pass its result to native flash after success; this helper is neither a session store nor an HTML renderer:
function notice(string $level, string $text): array
{
$level = match ($level) {
'error', 'danger' => 'danger',
'success', 'info', 'warning' => $level,
default => throw new \InvalidArgumentException('unsupported notice level'),
};
if ($text === '') {
throw new \InvalidArgumentException('empty notice');
}
return ['level' => $level, 'text' => $text];
}
Acceptance checklist
- Run POST→redirect→GET→refresh through real session middleware. A committed success renders once; refresh does not repeat it. Denial, validation failure, rollback and observer-vetoed save do not show success. Preserve exact redirect status/location.
- Check all allowed levels, error→danger mapping, unknown-level/empty-text rejection and accepted
"0". Two writes deliberately replace, not append; validate that choice with callers. - Render
<img src=x onerror=alert(1)>, ampersands, quotes and Unicode as text through actual Blade. No raw output, attacker-controlled CSS class or leaked private fields. Preserve accessible status/alert presentation. - Exercise malformed session payloads, missing middleware, multi-hop redirects, logout/rotation, two layout components, concurrent tabs/requests and separate users. State shared-session limitations without promising exactly-once delivery.
- Search
flash()helpers, views, service providers andcomposer why laracasts/flashbefore removal. Run request/session/template tests, recording versions, the multi-request trace and deliberate differences.
Sources
- Reference; MIT declared in composer.json:
src/Laracasts/Flash/FlashNotifier.php,Message.php,LaravelSessionStore.php,src/views/message.blade.php,tests/FlashTest.php. No standalone license file was present in this checkout; resolve notices before copying source. - Framework source checked:
src/Illuminate/Session/Store.php(flash,pull, aging) and Blade escaping. Reconcile the app's lockfile and actual middleware/driver. - Port options: check
get-recipe's current port catalog (sourced from Package Port) at implementation time.