One redirect notice in Laravel Rust
Scope
Replace one plain-text success/error/info/warning notification after a successful form action with native session flash data and an escaped application view. Exclude overlays, HTML message bodies, multiple-message queues, important/dismiss UI, macros and JS/toast APIs. The upstream Composer manifest declares MIT; retain applicable attribution/notices when adapting source. At the time of writing the port catalog listed a Rust gap; native sessions can still cover this bounded behavior. Check get-recipe's current port catalog. 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, and a falsy message updates the last message instead of adding one. Message.php::update filters falsy attributes. LaravelSessionStore.php delegates to the framework's flash method. src/views/message.blade.php outputs message bodies unescaped and forgets the notification key after rendering. tests/FlashTest.php checks accumulation, levels, overlays and store calls, not a real multi-request session lifecycle.
This recipe deliberately uses a new session key, one message (last write wins), escaped text, and rejects empty text while accepting the string "0". It does not reproduce falsy chaining or raw HTML. Reconcile callers and presentation rather than claiming drop-in compatibility.
Implementation
- Inventory the chosen producer/view, message codes/levels, redirects and middleware. Keep excluded queue/overlay/HTML callers on the package-backed path. Capture the existing redirect status/location and notice behavior before switching.
- Authorize the state change, enforce CSRF/validation and commit database work before recording success. Use fixed/trusted redirect destinations. Never display exception details, secrets or untrusted HTML as a notice. A flash is not an authorization decision or proof of persistence.
- Store a JSON-safe
{level,text}payload under a new literal key such asapp_noticeusingSession::flashon the request's session-enabled route. The inspected Rust session API supports serializable values and native one-next-request aging. Do not use ordinaryput, reflash every request or session-write from a queued job. - Consume once at the destination layout, pass plain text through the template's HTML autoescaping and map only allowed levels to fixed CSS classes. With the inspected API, pulling
serde_json::Valuethen validating the payload consumes even malformed values; typedpullleaves an undecodable value in place. Avoid two components independently consuming the same key. Never mark text as safe HTML. - Retain the existing session driver/cookie security and persistence-error policy. Concurrent requests/tabs share a session: native flash is not exactly-once cross-request delivery. Use existing request/session serialization if required and test middleware aging; do not introduce a new delivery subsystem for a cosmetic notice.
Complete pure payload constructor. It returns unescaped text to be escaped once by the destination template, not interpolated into HTML. The production action must store it through native flash only after success:
use serde_json::{Value, json};
fn notice(level: &str, text: &str) -> Result<Value, &'static str> {
let level = match level {
"error" | "danger" => "danger",
"success" => "success",
"info" => "info",
"warning" => "warning",
_ => return Err("unsupported notice level"),
};
if text.is_empty() { return Err("empty notice"); }
Ok(json!({"level": level, "text": text}))
}
Acceptance checklist
- POST success commits, flashes and redirects; the next GET renders once and a refresh does not repeat it. Validation/authorization/rollback failures never show success. Verify actual redirect status/location and middleware lifecycle, not only the constructor.
- Check info/success/warning/error (
dangeroutput), unknown-level rejection, empty-text rejection and accepted string"0". Two writes deliberately keep only the last notice. <img src=x onerror=alert(1)>, quotes, ampersands and Unicode render as text, never executable markup. Verify escaping in the actual template, a fixed class allowlist and accessible status/alert presentation.- Malformed session data does not leak or persist indefinitely. Test missing/session-disabled middleware, multiple destination components, intermediate redirects, logout/session rotation, parallel requests/tabs and two users without promising global exactly-once delivery.
- Search excluded producers/views and dependency consumers before removal. Run app session/request/template tests and record versions, exact request sequence, output and deliberate differences.
Sources
- Reference source; 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 redistribution notices before copying source. - Framework source checked:
crates/http/src/session/mod.rs(flash,pull, aging and session tests). Reconcile with installed framework/template versions. - Port options: check
get-recipe's current port catalog (sourced from Package Port) at implementation time.