正如Play 框架的官方文档所建议的那样,我们可以使用隐式request
来改进对与请求关联的某些对象的访问。
样本:
def index = Action { implicit request =>
session.get("connected").map { user =>
Ok("Hello " + user)
}.getOrElse {
Unauthorized("Oops, you are not connected")
}
}
这是由于添加了implicit
属于该Controller
特征的以下方法:
/**
* Retrieves the session implicitly from the request.
*
* For example:
* {{{
* def index(name:String) = Action { implicit request =>
* val username = session("username")
* Ok("Hello " + username)
* }
* }}}
*/
implicit def session(implicit request: RequestHeader) = request.session
问题:为什么这个方法被声明为implicit
?request
仅将参数声明为还不够implicit
吗?
我希望看到的是:
def session(implicit request: RequestHeader) = request.session