2012-08-12 87 views

回答

5

我問在play's discussion group和他們幫助我實現這個最初版本

我創建了一個PrefixedRequest這樣

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

然後,我用它在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添加this to application.conf

http.path=/prefix/ 

它似乎工作,但我沒有找到如何將這個前綴添加到相反的路線......任何人都可以在這部分手?

-

一些有用的鏈接

選中此threaddocs

+0

顯然,這些功能將不會包含在版本2.1之前 - https://github.com/playframework/Play20/commit/da6bbc4 – opensas 2012-08-13 01:32:45

+0

它可能對i18n很好(/ fr,/ en ...) – iwalktheline 2012-08-13 10:14:09

+1

我認爲你最好擴大http://www.playframework.org/documentation/api/2.0/scala/play/api/mvc/WrappedRequest.html – Drew 2012-10-16 13:05:10

25

在玩2.1,你可以做到這一點與在conf/application.conf以下選項:

application.context="/your/prefix" 

從遊戲2.4這個屬性叫做play.http.context(取自Gman的評論)。

+1

這比接受的答案要容易得多(這可能沒有奏效在播放2.0) – Nathan 2014-09-15 10:03:19

+0

這是正確的答案 – 2015-10-04 16:42:34

+3

只是讓其他人來自谷歌知道,在播放2.4它改爲'play.http.context'(見https://www.playframework.com/documentation/2.4。 X/Migration24) – Gman 2015-12-10 10:41:52