This package is to ease using flash message between your views
Update your Package.swift
file.
.Package(url: "https://github.com/nodes-vapor/flash", majorVersion: 1)
You can add the middleware either globally or to a route group.
import Flash
public func setup() throws {
// ...
addConfigurable(middleware: FlashMiddleware(), name: "flash")
}
Make sure both "sessions"
and "flash"
are present:
"middleware": [
"error",
"date",
"file",
"sessions",
"flash"
],
drop.group(FlashMiddleware()) { group in
// Routes
}
Apply flash on a response, which will be shown on next request
return Response(redirect: "/admin/users").flash(.error, "Failed to save user")
return Response(redirect: "/admin/users").flash(.success, "Successfuly saved")
return Response(redirect: "/admin/users").flash(.warning, "Updated user")
return Response(redirect: "/admin/users").flash(.info, "Email sent")
// Add to request by string
try request.flash.add(custom: String, message: String)
// Add to request by enum
try request.flash.add(type: Helper.FlashType, message: String)
// Clear all flashes
try request.flash.clear()
// Show current flash messages again in next request
try request.flash.refresh()
<!--Error-->
#if(request.storage._flash.error) {
<div class="alert alert-danger alert-dismissible fade in to-be-animated-in" role="alert">
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
<span class="fa fa-exclamation-circle"></span>
#(request.storage._flash.error)
</div>
}
<!--Success-->
#if(request.storage._flash.success) {
<div class="alert alert-success alert-dismissible fade in to-be-animated-in" role="alert">
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
<span class="fa fa-check-circle"></span>
#(request.storage._flash.success)
</div>
}
<!--Warning-->
#if(request.storage._flash.warning) {
<div class="alert alert-warning alert-dismissible fade in to-be-animated-in" role="alert">
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
#(request.storage._flash.warning)
</div>
}
<!--Info-->
#if(request.storage._flash.info) {
<div class="alert alert-info alert-dismissible fade in to-be-animated-in" role="alert">
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
#(request.storage._flash.info)
</div>
}
Add the flash html to one file and embed it in rest of your views or through a base layout
e.g.: #embed("Layout/Partials/Elements/alerts")
or
e.g.: #extend("Layout/Base-Layout")
This package is developed and maintained by the Vapor team at Nodes. The package owner for this project is Tom.
This package is open-sourced software licensed under the MIT license