I'm trying to make a route to a specific static file but everything I'm trying ends with an error.
I've made 3 different attempts:
1.
GET /file staticFile:/public/html/file.html
The error I get:
Compilation error
string matching regex `\z' expected but `:' found
2.
GET /file controllers.Assets.at(path="/public/html", "file.html")
The error I get:
Compilation error
Identifier expected
3.
GET /file controllers.Assets.at(path="/public/html", file="file.html")
The error I get: (and this is the weirdest)
Compilation error
not enough arguments for method at: (path: String, file: String)play.api.mvc.Call. Unspecified value parameter file.
The weird part about the 3rd error is that it's thrown in a different file (app/views/main.scala.html) on the following line:
<link rel="stylesheet" media="screen" href="@routes.Assets.at("stylesheets/main.css")">
All of these methods were found in the official documentation and/or threads here on stackoverflow. What am I missing here?
Thanks.
The solution to this that is cleanest is to create your own AssetsBuilder that will build your static page.
Create this file in your controllers package - Static.scala
Then in your routes you can define your static endpoint
Done. Now the file at
/public/html/file.html
will be served off oflocalhost:9000/file
If you replace the hard code above with a more generic:
then
/foo
will serve/public/html/foo.html
,/bar
will serve/public/html/bar.html
etc.I have the exact same issue. I followed the advice from @Jamil and managed to get this working for the static file (in my case a favicon) and managed to get the templates to compile but get a new error at runtime when trying to use the view. Relevant code below,
Change to the route (this route now resolves correctly)
Change to the view (compiles)
New error (only at runtime)
I know this isnt an answer but perhaps it will allow someone to pipe up with one.
IIRC, change
To
I am talking about your third attempt
Also, watch out for extra /
EDIT
Assuming your resource is at /public/stylesheets/main.css
I am not totally sure if this is correct, but this is what we are using to map a public folder containing our images, javascripts... etc..
This ability still haven't been added, as I know. But if someone needs answer, as an option, helper controller can be created for these purpose:
and then in routes config
I was experiencing the same issue while I was trying to configure some additional css. It worked with this syntax in the "routes" file