Scala Play Framework - controller as a class or singleton

I am trying to play 2.4.2 for Scala, and I do not understand whether controllers should be defined as classes or singlets. Status of documents:

A controller is nothing more than a singleton object that generates Action Values.

However, the sample code shows:

class Application extends Controller { ... } 

To complicate matters even further, intellij gives me a warning if I define a class:

intellij warning

However, I get a compilation error (but without warning) if I use singleton:

 package controllers import play.api._ import play.api.mvc._ object Application extends Controller { ... } 

Error: (6, -1) Play 2 Compiler: / Users / Toby / IdeaProjects / playscala / conf / routes: 6: type Application is not a member of package controllers

Which approach is right?

+7
scala playframework
source share
1 answer

Your controllers must be objects if you are using a static router. Static is the default router in Play 2.4 and has the same behavior in Play 2.3 and earlier.

You can convert your controllers into classes if you use an embedded router, which is new in Play 2.4. You need to enable the embedded router in build.sbt :

 routesGenerator := InjectedRoutesGenerator 

Update: The introduced router is now used by default in Play 2.5.

+10
source share

All Articles