Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

In play 1.x you had the http.path param which allowed you to set a url to add to every route

http.param

How can I achieve someting similar in play 2.0?

like image 504
opensas Avatar asked Aug 12 '12 19:08

opensas


People also ask

In which folder does play expect routes file?

The conf/routes file is the configuration file used by the Router. This file lists all the routes needed by the application.

How do you set content type explicitly on play?

Changing the default Content-Type Will automatically set the Content-Type header to text/plain , while: JsonNode json = Json. toJson(object); Result jsonResult = ok(json); will set the Content-Type header to application/json .

What is routes scala?

The routes file syntax conf/routes is the configuration file used by the router. This file lists all of the routes needed by the application. Each route consists of an HTTP method and URI pattern, both associated with a call to an Action generator.

What are routes in Java?

A Router is an actor that routes incoming messages to outbound actors. The router routes the messages sent to it to its underlying actors called 'routees'.


2 Answers

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).

like image 127
reen Avatar answered Oct 02 '22 03:10

reen


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

like image 42
opensas Avatar answered Oct 01 '22 03:10

opensas