1

I'm trying to dynamically define a version in the URI routing.

My immidiate ideas were to

a) I've configured in the application.conf a row stating my current version:

myApp.ver = 0.1

I wan't to use it in the routes file as part of the URI, for example:

GET /myApp/{version}/welcome controller.myApp.Welcome()

The idea is not to pass the version number to the Welcome() method but to get the version from the application.conf file.

I've searched for defining parameters in the route file but didn't find information that helped me.

b) Another idea was to have a variable in the routes, something like:

CurrentVersion = 0.1

in the routes file and use it in the URI.

How can I solve this? I havn't found an example for this.

Thanks.

Guy Wald
  • 599
  • 1
  • 10
  • 25

2 Answers2

2

If you want to do this for every route, it should be possible to set the application.baseUrl to include your version number. To do this conveniently you can define the version in your application.conf instead of the Build.scala file, as described here.

myApp.name = myApp
myApp.ver = 0.1
application.baseUrl = ${myApp.name}/${myApp.ver}

If you want to do it only for some routes, there doesn't seem to be a simple solution. If you could ignore parameters in routes, I'd say use a regexp parameter and verify it in your global router - unfortunately this doesn't seem possible without passing the parameter to the controller.

So I see two other options:

  1. Hardcode the version number in the routes file and do a search and replace every time it changes.
  2. Create a plugin for the sbt build process and let it replace the version in your routes file.
Community
  • 1
  • 1
kapex
  • 28,903
  • 6
  • 107
  • 121
1

In Play 1.2.x, in your conf/routes file, add a route like this:

GET   /myApp/${play.configuration.getProperty("myApp.ver")}/welcome     myApp.Welcome()
kapex
  • 28,903
  • 6
  • 107
  • 121
Manish Singh
  • 5,848
  • 4
  • 43
  • 31
  • That gives me: `sbt.PlayExceptions$RoutesCompilationException: Compilation error[Identifier expected]`. I don't think that you can use the same syntax as in templates. – kapex Aug 29 '13 at 20:33
  • We can use the same syntax as templates. I have verified that this works for me. I am assuming Play 1.2.x version. For 2.x you might need to do in a similar way. – Manish Singh Sep 25 '13 at 10:56
  • The question is tagged as play 2, I looked at the routes file parser of play 2 and didn't see any functionality for replacing variables like this. – kapex Sep 25 '13 at 11:10