Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scala Play 2, passing request to method

I have a Play 2.0 app

TestController.scala

def foo(p1: String) = Action {implicit request =>
  Ok(bar(p1))
}

private def bar(p1: String) = {
//access request parameter here
}

Is there a way to use implicit to pass request to bar

like image 493
Bob Avatar asked Jun 25 '12 23:06

Bob


1 Answers

Yes, you can:

  def foo(p1: String) = Action { implicit request =>
    Ok(bar(p1))
  }

  private def bar(p1: String)(implicit req: RequestHeader) = "content"

The code:

Action { implicit request

Calls this method on the Action object:

def apply(block: Request[AnyContent] => Result): Action[AnyContent] = {

So, what you called "request" matches the paramater named "block". The "implicit" here is optional: it makes the "request" value available as an implicit parameter to other method/function calls.

The implicit in your "bar" function says that it can take the value of "req" from an implicit value, and doesn't necessarily need to be passed in explicitly.

like image 118
Adam Rabung Avatar answered Sep 18 '22 19:09

Adam Rabung