How to add a prefix to all my routes in Play Framework 2?

In game 1.x, you had an http.path parameter that allowed you to set a URL to add to each route

http.param

How can I achieve similarities in game 2.0?

+7
source share
2 answers

I asked the discussion in the discussion group , and they helped me achieve this initial version.

I am creating a PrefixedRequest similar to 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)) } } 

The finale added this to application.conf

 http.path=/prefix/ 

This seems to work, but I can't find out how to add this prefix to the return routes ... can anyone give a hand for this part?

-

Some useful links

Check out this thread and docs

+5
source

In Play 2.1, you can do this 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 Gman's comment).

+25
source

All Articles