문제

I've written a Play 2.0 Plugin which redirects all incoming requests to a specific controller. We need this for template developing and testing. To achieve this my plugin hast a global object which looks like this:

object Global extends GlobalSettings {

  override def onRouteRequest(request: RequestHeader): Option[Handler] = {

    Play.application.plugin(classOf[MyPlugin]) match {
      case Some(plugin) => plugin.enabled() match {
        case true => Some(MyController.action)
      }
      case None => super.onRouteRequest(request)
    }

  }

}

The problem with this solution is, that I've no possibility to add additional logic to onRouteRequest in the main application.

  1. Is there another way to achieve this in the plugin then using onRouteRequest?
  2. Or is there a way to load the onRouteRequest plugin logic only when the plugin is enabled?
  3. Or is there another way to use two global objects with onRouteRequest?

Thanks for help

Torben

도움이 되었습니까?

해결책

A plugin should never, ever, ever define a Global, that's for the app.

Define a filter instead, and have the app include that in its filters:

object MyFilter extends EssentialFilter {
  def apply(next: RequestHeader => EssentialAction) = EssentialAction { request =>
    Play.application.plugin(classOf[MyPlugin]) match {
      case Some(plugin) if plugin.enabled() {
        MyController.action(request)
      case _ =>
        next(request)
    }
  }
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top