kornfridge kornfridge - 1 year ago 84
Scala Question

Using object SomeController extends Controller in Play framework 2.5 instead

We're in the progress of upgrading to play 2.5.5 from play 2.4.x. We have a
pretty large amount of controllers that are objects, like so:

object TranslationsController extends Controller { ... }

However, since switching to 2.5, we now get hundreds of errors like this
(it's a pretty huge project):

type TranslationsController is not a member of package controllers

Is there a way to still allow some/most our Controllers to be object and
not class? I realize this has everything to do with dependency injection.
But a pretty large percentage of the controllers, has no dependencies.

Of course, we could rewrite it like so just to make it compile:

class TranslationsController @Inject () extends Controller { ... }

but that's really pointless?? An immutable class/object that takes no
parameters, should be an object. It's like
case class None()
object None
. Of course the latter should be the preferred one in every case.

The big deal here is that we can't just string replace object->class
either, because we have a lot (~1000) of tests that assumes these
controllers to be objects. To make it work with class, we would have to
rewrite all those tests to first do a
val controller =
new TranslationsController
, first, which is a lot of work (and tons of

I would really appreciate if there was some way to still use object. Is
there some syntax for it in the
files, or is there an easy
workaround? I mean, once there are dependencies to these controllers, we'll
make them classes, but to do that to 100 controllers in one go without any
benefit is pretty painful.

Answer Source

You should be able to keep those controllers if you tell Play to use the old routes generator. In your build.sbt put:

routesGenerator := StaticRoutesGenerator

and reload your project

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download