Search code examples
scalaplayframeworkroutesimplicit

How to use imports and implicits in Play Framework's routes file?


What's the scope for routes file to find implicits like PathBindable or QueryStringBindable?

For custom types, it's trivial to simply define them in companion object like the following:

case class Foo(data: String)
object Foo {
  implicit val pathBinder: PathBindable[Foo] = ???
}

However, for existing types, it's unclear where to declare implicit to be found by routes file since we cannot do any custom import here.

So, What's the scope of implicits for routes file?


Solution

  • This doesn't directly answer the question, but it seems relevant...

    You can include custom imports in the routes file by adding to the routesImport key in your build.sbt

    For example:

    import play.PlayImport.PlayKeys._
    
    routesImport += "my.custom.package.Foo._"
    

    That snippet is borrowed from a blog post I wrote a while ago entitled Using Play-Framework's PathBindable