How can we call functions from other controllers in a play scala controller

916 Views Asked by At

In Play Scala 2.5 application, I define my controller like-

 class Application @Inject() (ws:WSClient) extends Controller{

Now, I want to call a function readConfig() of another controller class- ConfigReader Is there any way to do this, if i am not using services for this particular use cases of sharing functions in controllers?

In play 2.4, with controllers as objects, I could easily do it with Appliation.readConfig(). How can we do this with controllers as classes ?

1

There are 1 best solutions below

0
On

Just inject the controller instance:

If you have:

class ConfigReader @Inject() (ws:WSClient) extends Controller {
  def readConfig() = ???
}

You should be able to inject it into other controllers:

class Application @Inject() (ws:WSClient, configReader: controllers.ConfigReader) extends Controller {
  def get() = Action {
    configReader.readConfig()
  }
}