Created
September 20, 2017 06:59
-
-
Save wolfendale/75e8b5e9a7ace95aa7e6d123e6c6dacd to your computer and use it in GitHub Desktop.
Route Params in Composed Actions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
GET /:someParam MyController.foo(someParam) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment