Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Allow root context using [application.context] configuration setting.

  • Loading branch information...
commit da6bbc41f0bc3dcd0c6d6483180b84bac47dc0dc 1 parent a0f460d
@guillaumebort guillaumebort authored
View
10 framework/src/play/src/main/scala/play/api/Application.scala
@@ -88,7 +88,15 @@ class Application(val path: File, val classloader: ClassLoader, val sources: Opt
* The router used by this application (if defined).
*/
val routes: Option[Router.Routes] = try {
- Some(classloader.loadClass("Routes$").getDeclaredField("MODULE$").get(null).asInstanceOf[Router.Routes])
+ Some(classloader.loadClass("Routes$").getDeclaredField("MODULE$").get(null).asInstanceOf[Router.Routes]).map { router =>
+ router.setPrefix(configuration.getString("application.context").map { prefix =>
+ if(!prefix.startsWith("/")) {
+ throw configuration.reportError("application.context", "Invalid application context")
+ }
+ prefix
+ }.getOrElse("/"))
+ router
+ }
} catch {
case e: ClassNotFoundException => None
case e => throw e
View
26 framework/src/play/src/main/scala/play/core/router/Router.scala
@@ -251,6 +251,14 @@ object Router {
|
|object Routes extends Router.Routes {
|
+ |private var _prefix = "/"
+ |
+ |def setPrefix(prefix: String) {
+ | _prefix = prefix
+ |}
+ |
+ |def prefix = _prefix
+ |
|%s
|
|def routes:PartialFunction[RequestHeader,Handler] = {
@@ -386,7 +394,7 @@ object Router {
def genCall(route: Route, localNames: Map[String, String] = Map()) = " return _wA({method:\"%s\", url:%s%s})".format(
route.verb.value,
- route.path.parts.map {
+ "\"\"\"\" + Routes.prefix + " + { if(route.path.parts.isEmpty) "" else "{ if(Routes.prefix.endsWith(\"/\")) \"\" else \"/\" } + " } + "\"\"\"\" + " + route.path.parts.map {
case StaticPart(part) => "\"" + part + "\""
case DynamicPart(name, _) => {
route.call.parameters.getOrElse(Nil).find(_.name == name).map { param =>
@@ -394,7 +402,6 @@ object Router {
}.getOrElse {
throw new Error("missing key " + name)
}
-
}
}.mkString(" + "),
@@ -649,7 +656,7 @@ object Router {
def genCall(route: Route, localNames: Map[String, String] = Map()) = """Call("%s", %s%s)""".format(
route.verb.value,
- route.path.parts.map {
+ "Routes.prefix" + { if(route.path.parts.isEmpty) "" else """ + { if(Routes.prefix.endsWith("/")) "" else "/" } + """} + route.path.parts.map {
case StaticPart(part) => "\"" + part + "\""
case DynamicPart(name, _) => {
route.call.parameters.getOrElse(Nil).find(_.name == name).map { param =>
@@ -762,13 +769,13 @@ object Router {
case (r, i) =>
"""
|%s
- |val %s%s = Route("%s", %s)
+ |lazy val %s%s = Route("%s", %s)
""".stripMargin.format(
markLines(r),
r.call.packageName.replace(".", "_") + "_" + r.call.controller.replace(".", "_") + "_" + r.call.method,
i,
r.verb.value,
- "PathPattern(List(" + r.path.parts.map(_.toString).mkString(",") + "))")
+ "PathPattern(List(StaticPart(Routes.prefix)" + { if(r.path.parts.isEmpty) "" else """,StaticPart(if(Routes.prefix.endsWith("/")) "" else "/"),""" } + r.path.parts.map(_.toString).mkString(",") + "))")
}.mkString("\n") +
"""|
|def documentation = List(%s)
@@ -951,8 +958,8 @@ object Router {
case chars => StaticPart(chars.mkString)
}
- def path: Parser[PathPattern] = ((positioned(singleComponentPathPart) | positioned(multipleComponentsPathPart) | positioned(regexComponentPathPart) | staticPathPart) +) ^^ {
- case parts => PathPattern(parts)
+ def path: Parser[PathPattern] = "/" ~ ((positioned(singleComponentPathPart) | positioned(multipleComponentsPathPart) | positioned(regexComponentPathPart) | staticPathPart) *) ^^ {
+ case _ ~ parts => PathPattern(parts)
}
def parameterType: Parser[String] = ":" ~> ignoreWhiteSpace ~> rep1sep(identifier, ".") ~ opt(brackets) ^^ {
@@ -1108,10 +1115,15 @@ object Router {
}
trait Routes {
+ self =>
def documentation: Seq[(String, String, String)]
def routes: PartialFunction[RequestHeader, Handler]
+
+ private[play] def setPrefix(prefix: String)
+
+ def prefix: String
//
Please sign in to comment.
Something went wrong with that request. Please try again.