Skip to content

Instantly share code, notes, and snippets.

@eboto
Created November 26, 2012 19:58
Show Gist options
  • Save eboto/4150259 to your computer and use it in GitHub Desktop.
Save eboto/4150259 to your computer and use it in GitHub Desktop.
Play 2.1 reverse route behavior with a module and an application that depends on it

Sample directory structure and file-contents for a one-module, one-app play build. It illustrates how routes compilation and resolution might work in a way that avoids namespace collision on the controllers.routes symbol (a bug in Play 2.0.x).

The initial version of this gist demonstrates the behavior of the pull request that solves that bug, authored by @jroper:

Though this solves a pretty huge problem in our company, the reverse router
syntax is a bit unsatisfying due to the awkward syntax of module reverse routes: controllers.module.module.routes.Module

Can that be improved without introducing more opportunity for namespace collision?

application/
  app/controllers/Application.scala
    | // Controllers for the application
    | package controllers
    |
    | object Application {
    | 
    |   def index = Ok("Welcome to the application")
    |   
    |   // v-- module.module.routes.Module is unsatisfying
    |   def moduleIndex = Redirect(controllers.module.module.routes.Module.index)
    | }

  conf/routes
    | # Application routes
    | GET / controllers.Application.index
    | 
    | -> /module module.Routes

module/
  app/controllers/module/Module.scala
    | // Controllers for the module, responsibly namespaced so that they
    | // can be good citizen when included in other projects.
    | package controllers.module
    | 
    | object Module {
    |   def index = Ok("Welcome to the Module")
    | }

  conf/module.routes
    | # routes for the module. Excluding an Assets route
    | # to keep the example simple. Maybe I should include it?
    |
    | GET / controllers.module.Module.index

Upon routes compilation, this project layout produces two routes.java files:

module/target/scala-2.10/src_managed/main/controllers/module/module/routes.java
  | // Generated by module/conf/module.routes. The path unsatisfyingly resolves 
  | // to module/module because conf/module.routes contained a reference to
  | // controllers.module.Module.
  | //
  | // It is this location that requires app/controllers/Application.scala to reference
  | // the reverse router as controllers.module.module.routes.
  | //
  | // Can we fix that without introducing further namespace collisions?
  | 
  | package module.module
  |
  | public class routes { /* impl */ }

application/target/scala-2.10/src_managed/main/controllers/routes.java
  | // Generated by application/conf/routes
  | package controllers
  | public class routes { /* impl */ }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment