Skip to content

Instantly share code, notes, and snippets.

@wolfendale
Created September 20, 2017 06:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wolfendale/75e8b5e9a7ace95aa7e6d123e6c6dacd to your computer and use it in GitHub Desktop.
Save wolfendale/75e8b5e9a7ace95aa7e6d123e6c6dacd to your computer and use it in GitHub Desktop.
Route Params in Composed Actions
package com.example
import javax.inject.Inject
import play.api.i18n.MessagesApi
import play.api.mvc._
import scala.concurrent.Future
// note: `messagesApi` could be anything, it's just quite likely that your controller
// will require things other than what is being passed from the route
class MyAction(someParam: String, messagesApi: MessagesApi) extends ActionBuilder[Request] {
override def invokeBlock[A](request: Request[A], block: (Request[A]) => Future[Result]): Future[Result] = {
// do something with someParam
block(request)
}
}
class MyActionFactory @Inject() (messagesApi: MessagesApi) {
def apply(someParam: String): MyAction = new MyAction(someParam, messagesApi)
}
package com.example
import javax.inject.Inject
import play.api.mvc._
class MyController @Inject() (myAction: MyActionFactory) extends Controller {
def foo(someParam: String): Action[AnyContent] = myAction(someParam) {
implicit request =>
NoContent
}
}
GET /:someParam MyController.foo(someParam)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment