Scala Play Framework - 控制器作为 class 或单例

Scala Play Framework - controller as class or singleton

我正在为 Scala 尝试 Play 2.4.2,但我不清楚控制器是否应该定义为 classes 或单例。文档状态:

A Controller is nothing more than a singleton object that generates Action values.

但是代码示例显示:

class Application extends Controller { ... }

更复杂的是,如果我定义 class:

,intellij 会发出警告

但是,如果我使用单例,则会出现编译错误(但没有警告):

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

哪种方法是正确的?

如果您使用静态路由器,您的控制器应该是对象。 static 是 Play 2.4 中的默认路由器,与 Play 2.3 及之前的版本具有相同的行为。

如果您使用 Play 2.4 中新增的注入路由器,您可以将控制器转换为 类。您需要在 build.sbt:

中启用注入的路由器
routesGenerator := InjectedRoutesGenerator

更新:注入的路由器现在是 Play 2.5 中的默认路由器