2

I am working on a Play Framework 2.2 application that serves both a JSON api and a single-page application. For the single-page app, I am using Backbone.js, and I would like to support the HTML5 History api.

Currently, Play is serving the single-page app via an index.html file, and all routing in the single-page app is done with hash-based routing. Below is the route that I am using:

GET   /app/*file   controllers.Assets.at(path="/public/app/src", file)

And a sample URL that is handled by the single-page app is:

/app/index.html#/some/url

I would like to be able to route all URLS that begin with /app/ to my index.html file, so that the above URL would become the following and would still be handled by my index.html file:

/app/some/url

My idea was to use a route like the following:

GET   /app/*anything   controllers.Assets.at(path="/public/app/src", file="index.html")

However, Play is not happy with me not using the "anything" route, so I get the following compilation error: Missing parameter in call definition: anything.

Is there any way in Play 2 to have a "wildcard" route like the one above route to a single, static file?

Andrew
  • 2,084
  • 20
  • 32

1 Answers1

4

This is a limitation in Play, but one that's easy to work around. Define an action that delegates to the assets controller and accepts a parameter that it ignores:

def index(path: String) = controllers.Assets.at(path="/public/app/src", file="index.html")

Then point your route at that.

James Roper
  • 12,695
  • 46
  • 45
  • That works great! This seems like a common enough case that the play router should accommodate it, but I couldn't ask for a simpler work-around. Thank you! – Andrew Aug 19 '14 at 03:46