Search code examples
scalaslickplayframework-2.3implicits

How is Playframework's Session injected into the Action?


I am trying to write my own action and pass in a DatabaseSession implicitly. However, at best I can do something like this in my controller.

def index = MyAction { implicit myRequest =>
  implicit val dbss = myRequest.databaseSession
  aClass.someMethod() // requires an implicit DatabaseSession
}

In playframework, you can access the session like thus:

def index = Action { implicit request =>
  val someOption = session.get("something")
  // OR
  aClass.doSomething() // requires an implicit Session
}

Here, as we can see, you could directly access the session, when only the request is passed in as implicit. So where is the session coming from? And how would I be able to pass in my DatabaseSession just like Session? So that I don't have to write:

implicit val dbss = myRequest.databaseSession

I know this is possible, because slick is able to pass in their dbSession implicitly. But I can't seem to work out how they do it either.

https://github.com/playframework/play-slick/blob/master/code/src/main/scala/play/api/db/slick/DBAction.scala

Totally confused! =S


Solution

  • After some hard digging, I found the solution to my own question.

    The secret lies in the Controller, which has an implicit def:

    implicit def request2session(implicit request: RequestHeader): Session
    

    PlayFramework, you're smart!