Warm tip: This article is reproduced from stackoverflow.com, please click
playframework scala

Where is the object Action in play.api.mvc?

发布于 2020-04-23 18:10:03

I am a novice of Play Framework. When I learn it on its webpages. I found some code like this:

import play.api.mvc._
def logging[A](action: Action[A]) = Action.async(action.parser) { request =>
  logger.info("Calling action")
  action(request)
}

I checked its document and there is a function async in ActionBuilder.

How does Action.async works? It seems there is no object Action in play.api.mvc

Questioner
Confixuan
Viewed
16
Mario Galic 2020-02-12 18:45

object Action has been removed in Play 2.8 by Remove deprecated play.api.mvc.Action #9288, and has been replaced by BaseController.Action method which refers to injected controllerComponents.actionBuilder rather than the global objects

  /**
   * ...
   * This is meant to be a replacement for the now-deprecated Action object, and can be used in the same way.
   */
  def Action: ActionBuilder[Request, AnyContent] = controllerComponents.actionBuilder

Notice how, perhaps unconventionally, the method name begins with an uppercase letter. My assumption is this was done to maintain familiar usage

def foo(query: String) = Action {
  Ok
}