Reputation:
I'm trying Play 2.4.2 for Scala and I'm not clear whether controllers should be defined as classes or singletons. The docs state:
A Controller is nothing more than a singleton object that generates Action values.
However the code sample shows:
class Application extends Controller { ... }
To further complicate things, intellij gives me a warning if I define a class:
However I get a compilation error (but no warning) if I use a singleton:
package controllers
import play.api._
import play.api.mvc._
object Application extends Controller { ... }
Error:(6, -1) Play 2 Compiler: /Users/Toby/IdeaProjects/play-scala/conf/routes:6: type Application is not a member of package controllers
Which approach is correct?
Upvotes: 7
Views: 2580
Reputation: 3519
Your controllers should be objects if you use the static router. The static is the default router in Play 2.4 and has the same behavior from Play 2.3 and before.
You can convert your controllers to classes if you use the injected router which is new in Play 2.4. You need to enable the injected router in your build.sbt
:
routesGenerator := InjectedRoutesGenerator
Update: the injected router is now the default in Play 2.5
Upvotes: 10