Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>There have been changes in play 2.2 to make it easier to compose actions. The resource you are referring to is outdated. </p> <p>Instead you should create a custom action builder by extending ActionBuilder to create your action, this will get you all the fancy apply methods you may need (including async support and all)</p> <p>For example you may do : </p> <pre><code>trait MyAction extends Results{ class MyActionBuilder[A] extends ActionBuilder[({ type R[A] = Request[A] })#R] { def invokeBlock[A](request: Request[A], block: Request[A] =&gt; Future[SimpleResult]) ={ // your authentication code goes here : request.cookies.get("loggedIn").map { _=&gt; block(request) } getOrElse Future.successful(Unauthorized) } } object MyAction extends MyActionBuilder } </code></pre> <p>which you can then use as such : </p> <pre><code>object MyController extends Controller with MyAction{ def authenticatedAction=MyAction { Ok } def asyncAuthenticatedAction=MyAction.async { Future.successful(Ok) } def authenticatedActionWithBodyParser = MyAction(parse.json){ request =&gt; Ok(request.body) } } </code></pre> <p>For brevity's sake I used a very trivial authentication mechanism you will want to change that :) </p> <p>Additionally, you can create a custom "request" type to provide additional information. For instance you could define a AuthenticatedRequest as such : </p> <pre><code> case class AuthenticatedRequest[A](user: User, request: Request[A]) extends WrappedRequest(request) </code></pre> <p>Provided you have a way to get your user such as </p> <pre><code> object User{ def find(s:String): Option[User] = ??? } </code></pre> <p>Then change your builder definition a bit as such </p> <pre><code> class MyActionBuilder[A] extends ActionBuilder[({ type R[A] = AuthenticatedRequest[A] })#R] { def invokeBlock[A](request: Request[A], block: AuthenticatedRequest[A] =&gt; Future[SimpleResult]) ={ // your authentication code goes here : (for{ userId &lt;- request.cookies.get("userId") user &lt;- User.find(userId.value) }yield { block(AuthenticatedRequest(user,request)) }) getOrElse Future.successful(Unauthorized) } } </code></pre> <p>Your controller now has access to your user in authenticatedActions: </p> <pre><code>object MyController extends Controller with MyAction{ val logger = Logger("application.controllers.MyController") def authenticatedAction=MyAction { authenticatedRequest =&gt; val user = authenticatedRequest.user logger.info(s"User(${user.id} is accessing the authenticatedAction") Ok(user.id) } def asyncAuthenticatedAction = MyAction.async { authenticatedRequest=&gt; Future.successful(Ok(authenticatedRequest.user.id)) } def authenticatedActionWithBodyParser = MyAction(parse.json){ authenticatedRequest =&gt; Ok(authenticatedRequest.body) } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload