Blankman
Blankman

Reputation: 266900

Creating a prototype of play's controller and action to help understand scala

I'm confused as to how the request object can be injected into the Action.

Was hoping someone could create a simple prototype of the following in scala:

class HomeController() extends AbstractController {
  def index() = Action { request =>
    Ok("hello")
  }
}

What I mean is, create the above classes/functions to simple return a string "hello", with the ability to get other objects in scope like "request".

abstract class AbstractController()
case class Action(???)
case class Ok(????)

I am just confused has to how you can create an Action {} and then have request available in the block specifically.

Upvotes: 1

Views: 36

Answers (1)

Aki
Aki

Reputation: 1754

If you write Action { request => ??? }, you're calling the apply method in the Action companion object. This method takes one parameter which is a function that takes a request and returns a response. The request value is the parameter of the function that you pass to the apply method.

Here's the method that you're calling.

If you would write a class like Action yourself, it may look similar to this:

case class Action(f: Request => Ok)

Upvotes: 1

Related Questions