Reputation: 63395
In play 1.x you had the http.path param which allowed you to set a url to add to every route
How can I achieve someting similar in play 2.0?
Upvotes: 9
Views: 5205
Reputation: 2502
In Play 2.1 you can do that with the following option in conf/application.conf
:
application.context="/your/prefix"
From Play 2.4 this property is called play.http.context
(taken from the comment by Gman).
Upvotes: 32
Reputation: 63395
I asked at play's discussion group and they helped me achieve this initial version
I create a PrefixedRequest like this
import play.api.mvc.RequestHeader
import play.api.Play.configuration
import play.api.Play.current
class PrefixedRequest(request: RequestHeader) extends RequestHeader {
def headers = request.headers
def queryString = request.queryString
// strip first part of path and uri if it matches http.path config
def path = ("^" + prefix).r.replaceFirstIn(request.path, "/")
def uri = ("^" + prefix).r.replaceFirstIn(request.uri, "/")
def method = request.method
def remoteAddress = request.remoteAddress
lazy val prefix = {
val config = configuration.getString("http.path").getOrElse("")
if (config.endsWith("/")) config else config + "/"
}
}
object PrefixedRequest {
def apply(request: RequestHeader) = new PrefixedRequest(request)
}
Then I used it in Global.scala
import play.api.GlobalSettings
import play.api.mvc.RequestHeader
import play.api.mvc.Handler
object Global extends GlobalSettings {
override def onRouteRequest(request: RequestHeader): Option[Handler] = {
super.onRouteRequest(PrefixedRequest(request))
}
}
finnally added this to application.conf
http.path=/prefix/
It seems to work, but I couln't find out how to add that prefix to the reversed routes... can anybody gimme a hand on that part?
--
Some useful links
Check this thread and the docs
Upvotes: 6