nodes-vapor / flash Goto Github PK
View Code? Open in Web Editor NEWFlash messages between views ⚡️
License: MIT License
Flash messages between views ⚡️
License: MIT License
I'd love to be able to throw an Abort.redirect(…)
, and have it automatically transmit a flash to the page I'm redirecting to.
Following the documentation, double checking the configuration and requirements. I even created a new project just to configure flash and have one route that does nothing but redirect to the next one whilst adding a flash message.
configure.swift
import Leaf
import Vapor
import Flash
/// Called before your application initializes.
public func configure(_ config: inout Config, _ env: inout Environment, _ services: inout Services) throws {
/// Register providers first
try services.register(LeafProvider())
try services.register(FlashProvider())
/// Register routes to the router
let router = EngineRouter.default()
try routes(router)
services.register(router, as: Router.self)
/// Use Leaf for rendering views
config.prefer(LeafRenderer.self, for: ViewRenderer.self)
/// Register middleware
var middlewares = MiddlewareConfig() // Create _empty_ middleware config
middlewares.use(FileMiddleware.self) // Serves files from `Public/` directory
middlewares.use(ErrorMiddleware.self) // Catches errors and converts to HTTP response
middlewares.use(SessionsMiddleware.self)
middlewares.use(FlashMiddleware.self)
services.register(middlewares)
}
routes.swift
import Vapor
import Flash
public func routes(_ router: Router) throws {
router.get { req in
return try req.view().render("welcome")
}
router.get("hello") { req -> Future<Response> in
return req.future(req.redirect(to: "/").flash(.error, "Hey Listen!"))
}
}
welcome.leaf
#set("title") { It works }
#set("body") {
<div class="welcome">
<div class="alerts">
#flash() {
#for(flash in all) {
Message: #(flash.message)
Type: #(flash.kind)
}
}
</div>
<img src="/images/it-works.png">
</div>
}
#embed("base")
Putting a breakpoint in the package at the check whether flashes
isEmpty shows, it is empty.. 🤔
My impression of flash messages would be to show them only once. In this implementation, I see them only get added but not removed. Am I doing something wrong or am I missing something?
Otherwise, my suggestion would be to remove them when being rendered, so to insert the following line into FlashTag.swift
at the end of the function render
:
flash.flashes.removeAll()
Sort out folder structure (align with other packages) and split code into files when needed.
@dennishn can you make a PR with the CSS / js for the flash message html in readme
Vapor requires a release 4.0.1 to solve dependencies properly. I forked, published the release, and it works fine.
Please just release 4.0.1 so that it will compile properly.
Thanks
The following codes won't work, the error message is No tag named `x` is registered.
services.register { container -> LeafTagConfig in
var leafTagConfig = LeafTagConfig.default()
leafTagConfig.use(XLeafTagRenderer(), as: "x")
leafTagConfig.use(FlashTag(), as: "flash")
return leafTagConfig
}
try services.register(FlashProvider())
The following codes won't work, the error message is No tag named `flash` is registered.
services.register { container -> LeafTagConfig in
var leafTagConfig = LeafTagConfig.default()
leafTagConfig.use(XLeafTagRenderer(), as: "x")
return leafTagConfig
}
try services.register(FlashProvider())
The following codes will work.
try services.register(FlashProvider())
services.register { container -> LeafTagConfig in
var leafTagConfig = LeafTagConfig.default()
leafTagConfig.use(XLeafTagRenderer(), as: "x")
leafTagConfig.use(FlashTag(), as: "flash")
return leafTagConfig
}
So, I think there is no point in importing MutableLeafTagConfig
Are there any plans for Vapor 3 compatibility?
Update dependencies when Vapor 3 gets tagged
According to Tanner, this forces the template to rerender on every route call.
Hey so I followed all of the instructions. I'm trying to add this globally. No matter what I do I can't seem to get the flash messages to render in my HTML. I copied and pasted the code from the README. Is there a way I can debug what is going on? I don't get any errors or anything. Thanks.
Find a way to simulate a session middleware
Right now Response().flash(.success, "Success) cannot be used with another type in same Response
This is do to the override of storage in the extension
Now that Vapor 3 is nearing a final release, what does the status look like for updating this library? I am more than happy to write the Vapor 3 version if someone can help point me in the right direction with request.storage
not being around anymore.
Make tests for v 2.0 (Vapor 3)
Make docs for v 2.0 (Vapor 3)
Now that Vapor 2 is released I'd love to see an official 1.0 tag supporting Vapor 2.
This package is awesome! I use it in all my projects. Kudos.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.