user2066049
user2066049

Reputation: 1371

abstract out request in play controller

I see repeating code like following in many methods in a play contorller. since request is available in the Actions, any way to abstract this out of all the methods to avoid repetation?

def serveData = Action { implicit request =>
  val host = "http://" + request.host 

Upvotes: 1

Views: 34

Answers (1)

serejja
serejja

Reputation: 23881

You can do something like this:

def withHost(f: String => SimpleResult) = Action { implicit request =>
  val host = "http://" + request.host
  f(host)
}

def serveData = withHost { host =>
  Ok(host)
}

Upvotes: 1

Related Questions