Expand description
Flash messages middleware for the Salvo web framework.
Flash messages are temporary notifications that persist across a single redirect, commonly used to display success, error, or info messages after form submissions.
§How It Works
- A handler sets flash messages before redirecting
- The middleware stores them (in cookies or session)
- On the next request, the messages are available and then cleared
§Storage Options
| Store | Feature | Description |
|---|---|---|
CookieStore | cookie-store | Stores messages in a cookie |
SessionStore | session-store | Stores messages in the session |
§Message Levels
Messages have severity levels for appropriate UI styling:
Debug- Debugging informationInfo- General informationSuccess- Operation completed successfullyWarning- Warning that doesn’t prevent operationError- Error that needs attention
§Example
ⓘ
use salvo_flash::{FlashDepotExt, CookieStore};
use salvo_core::prelude::*;
#[handler]
async fn submit_form(depot: &mut Depot, res: &mut Response) {
// Set flash message before redirect
depot.outgoing_flash_mut().success("Form submitted successfully!");
res.render(Redirect::other("/result"));
}
#[handler]
async fn show_result(depot: &mut Depot, res: &mut Response) {
if let Some(flash) = depot.incoming_flash() {
for msg in flash.iter() {
println!("{}: {}", msg.level, msg.value);
}
}
res.render("Result page");
}
// Setup router with flash middleware
let router = Router::new()
.hoop(CookieStore::new().into_handler())
.push(Router::with_path("submit").post(submit_form))
.push(Router::with_path("result").get(show_result));§Filtering by Level
Use minimum_level() to filter out lower-priority messages:
ⓘ
let mut handler = FlashHandler::new(CookieStore::new());
handler.minimum_level(FlashLevel::Warning); // Only show Warning and ErrorRead more: https://salvo.rs
Structs§
- Cookie
Store cookie-store - CookieStore is a
FlashStoreimplementation that stores the flash messages in a cookie. - Flash
- A flash is a list of messages.
- Flash
Handler FlashHandleris a middleware for flash messages.- Flash
Message - A flash message.
- Session
Store session-store - SessionStore is a
FlashStoreimplementation that stores the flash messages in a session.
Enums§
- Flash
Level - Verbosity level of a flash message.
Constants§
- INCOMING_
FLASH_ KEY - Key for incoming flash messages in depot.
- OUTGOING_
FLASH_ KEY - Key for outgoing flash messages in depot.
Traits§
- Flash
Depot Ext - A trait for
Depotto get flash messages. - Flash
Store FlashStoreis for stores flash messages.
Functions§
- cookie_
store cookie-store - Helper function to create a
CookieStore. - session_
store session-store - Helper function to create a
SessionStore.