Skip to content
This repository has been archived by the owner on Apr 20, 2024. It is now read-only.

It doesn't work #32

Closed
martinlasek opened this issue Sep 20, 2018 · 3 comments
Closed

It doesn't work #32

martinlasek opened this issue Sep 20, 2018 · 3 comments
Assignees
Labels

Comments

@martinlasek
Copy link
Contributor

martinlasek commented Sep 20, 2018

The Flash package doesn't seem to work.

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.. 🤔

@steffendsommer
Copy link
Contributor

steffendsommer commented Sep 21, 2018

router.get { req in
    return try req.view().render("welcome")
}

should be

router.get { req in
    return try req.privateContainer.view().render("welcome")
}

Discussed here #26 and is something @siemensikkema has been looking into. I'm surprised that it is not mentioned in the readme - this should be added even though the requirement hopefully goes away soon.

@martinlasek
Copy link
Contributor Author

@steffendsommer thanks for the hint! Actually privateContainer doesn't have view() 🤔
But it was a good hint to grasp it and get it working with:

func renderRegister(req: Request) throws -> Future<View> {
  let viewData = try ViewData.appInfoWithKey(on: req)
  let vr = try req.privateContainer.make(LeafRenderer.self)
  return vr.render("User/register", viewData)
}

👍

@tonyarnold
Copy link
Contributor

This is still an issue - everything is setup, and nothing renders:

private func loginHandler(_ request: Request) throws -> Future<View> {
     return try request.view().render("login", on: request)
}

Has anything changed relating to this issue in the past year? I can see work going on, but the instructions in the readme seem incorrect, and #29/#34 haven't been merged.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

5 participants