§Aggregating reverse routers
In some situations you want to share reverse routers between sub projects that are not dependent on each other.
For example, you might have a
web sub project, and an
api sub project. These sub projects may have no dependence on each other, except that the
web project wants to render links to the
api project (for making AJAX calls), while the
api project wants to render links to the
web (rendering the web link for a resource in JSON). In this situation, it would be convenient to use the reverse router, but since these projects don’t depend on each other, you can’t.
Play’s routes compiler offers a feature that allows a common dependency to generate the reverse routers for projects that depend on it so that the reverse routers can be shared between those projects. This is configured using the
aggregateReverseRoutes sbt configuration item, like this:
lazy val common: Project = (project in file("common")) .enablePlugins(PlayScala) .settings( aggregateReverseRoutes := Seq(api, web) ) lazy val api = (project in file("api")) .enablePlugins(PlayScala) .dependsOn(common) lazy val web = (project in file("web")) .enablePlugins(PlayScala) .dependsOn(common)
In this setup, the reverse routers for
web will be generated as part of the
common project. Meanwhile, the forwards routers for
web will still generate forwards routers, but not reverse routers, because their reverse routers have already been generated in the
common project which they depend on, so they don’t need to generate them.
Note that the
commonproject has a type of
Projectexplicitly declared. This is because there is a recursive reference between it and the
webprojects, through the
aggregateReverseRoutessetting, so the Scala type checker needs an explicit type somewhere in the chain of recursion.
Next: Improving Compilation Times
Found an error in this documentation? The source code for this page can be found here. After reading the documentation guidelines, please feel free to contribute a pull request. Have questions or advice to share? Go to our community forums to start a conversation with the community.