public
Last active

Play resource routing

  • Download Gist
MyManagedResource.scala
Scala
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
class MyManagedResource extends ResourceController[String] {
def index = Action(Ok("index"))
 
def newScreen = Action(Ok("new"))
 
def create = Action {
Redirect(MyInjectableResource.reverseRoutes.index())
}
 
def show(id: String) = Action(Ok("Show " + id))
 
def edit(id: String) = Action(Ok("Edit " + id))
 
def update(id: String) = Action(Ok("Update " + id))
 
def destroy(id: String) = Action(Ok("Destroy " + id))
 
def custom(id: String) = Action(Ok("Custom " + id))
}
 
object MyManagedResource extends ManagedResourceRouter[String, MyManagedResource]
MyResource.scala
Scala
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
package controllers
 
import play.api.mvc._
 
object MyResource extends ResourceRouter[Long] {
def index = Action(Ok("index"))
 
def newScreen = Action(Ok("new"))
 
def create = Action {
Redirect(reverseRoutes.index())
}
 
def show(id: Long) = Action(Ok("Show " + id))
 
def edit(id: Long) = Action(Ok("Edit " + id))
 
def update(id: Long) = Action(Ok("Update " + id))
 
def destroy(id: Long) = Action(Ok("Destroy " + id))
 
def custom(id: Long) = Action(Ok("Custom " + id))
}
ResourceController.scala
Scala
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
package play.api.mvc
 
import play.api.mvc._
import play.core.Router
import scala.runtime.AbstractPartialFunction
import scala.reflect.ClassTag
import play.api.Play
import play.core.Router.JavascriptReverseRoute
 
trait ResourceController[T] extends Controller {
 
/**
* Get the index for the resource.
*
* Typically this might be a list of interesting resources.
*
* This is invoked for GET requests on /myresource.
*/
def index: EssentialAction
 
/**
* Render the new screen page.
*
* Typically this will mean rendering an HTML form.
*
* This is invoked for GET requests on /myresource/new.
*/
def newScreen: EssentialAction
 
/**
* Create a new resource.
*
* This is invoked for POST requests on /myresource.
*/
def create: EssentialAction
 
/**
* Get a given resource.
*
* This is invoked for GET requests on /myresource/id.
*
* @param id The id of the resource to get.
*/
def show(id: T): EssentialAction
 
/**
* Render the edit page for a given resource.
*
* Typically this will mean rendering an HTML form, prepopulated with the details of the resource.
*
* This is invoked for GET requests on /myresource/edit.
*
* @param id The id of the resource to edit.
*/
def edit(id: T): EssentialAction
 
/**
* Update the resource with the given id.
*
* This is invoked for PUT requests on /myresource/id.
*
* @param id The id of the resource to update.
*/
def update(id: T): EssentialAction
 
/**
* Delete the resource with the given id.
*
* This is invoked for DELETE requests on /myresource/id
*
* @param id The id of the resource to delete.
*/
def destroy(id: T): EssentialAction
}
 
trait DefaultResourceController[T] extends ResourceController[T] {
def index = ???
def newScreen = ???
def create = ???
def show(id: T) = ???
def edit(id: T) = ???
def update(id: T) = ???
def destroy(id: T) = ???
}
 
/**
* A resource router. Typically it will be implemented by an object, like so:
*
* {{{
* object MyResource extends ResourceRouter[Long] {
* def index = Action {
* ...
* }
* def create(id: Long) = Action {
* ...
* }
*
* ...
* }
* }}}
*
* Then from a routes file can be used like so:
*
* {{{
* -> /myresource controllers.MyResource
* }}}
*
* @param name A custom name for the resource controller. This is used by the Javascript router.
* @param idBindable The bindable for converting the id to the expected type.
* @tparam T
*/
abstract class ResourceRouter[T](name: Option[String] = None)(implicit idBindable: PathBindable[T]) extends Router.Routes with ResourceController[T] {
self =>
 
private val controllerName = name.getOrElse {
val name = self.getClass.getName
if (name.endsWith("$")) {
name.dropRight(1)
} else {
name
}
}
 
private var path: String = ""
 
private val MaybeSlash = "/?".r
private val Id = "/([^/]+)".r
private val Edit = "/([^/]+)/edit".r
 
def routes = new AbstractPartialFunction[RequestHeader, Handler] {
def withId(id: String, action: T => EssentialAction) = idBindable.bind("id", id).fold(badRequest, action)
 
override def applyOrElse[A <: RequestHeader, B >: Handler](rh: A, default: A => B) = {
if (rh.path.startsWith(path)) {
(rh.method, rh.path.drop(path.length)) match {
case ("GET", MaybeSlash()) => index
case ("GET", "/new") => newScreen
case ("POST", MaybeSlash()) => create
case ("GET", Id(id)) => withId(id, show)
case ("GET", Edit(id)) => withId(id, edit)
case ("PUT", Id(id)) => withId(id, update)
case ("DELETE", Id(id)) => withId(id, destroy)
case _ => default(rh)
}
} else {
default(rh)
}
}
 
def isDefinedAt(rh: RequestHeader) = if (rh.path.startsWith(path)) {
(rh.method, rh.path.drop(path.length)) match {
case ("GET", MaybeSlash()) => true
case ("GET", "/new") => true
case ("POST", MaybeSlash) => true
case ("GET", Id(id)) => true
case ("GET", Edit(id)) => true
case ("PUT", Id(id)) => true
case ("DELETE", Id(id)) => true
case _ => false
}
} else {
false
}
 
}
 
def setPrefix(prefix: String) {
path = prefix
}
 
def prefix = path
 
def documentation = Seq(
("GET", path, name + ".index"),
("GET", path + "/new", name + ".newScreen"),
("POST", path, name + ".create"),
("GET", path + "/$id<[^/]+>", name + ".show(id: T)"),
("GET", path + "/$id<[^/]+>/edit", name + ".edit(id: T)"),
("PUT", path + "/$id<[^/]+>", name + ".update(id: T)"),
("DELETE", path + "/$id<[^/]+>", name + ".destroy(id: T)")
)
 
def reverseRoutes: ResourceReverseRouter[T] = new ResourceReverseRouter[T](path)
 
def jsRoutes: ResourceJavascriptReverseRouter[T] = new ResourceJavascriptReverseRouter[T](controllerName, path)
}
 
/**
* A resource router that uses managed controllers.
*
* Typically it will be implemented by an object, supplying a controller class that should be managed by the
* Global.getControllerInstance method. For example:
*
* {{{
* class MyResource(dbService: DbService) extends ResourceController[Long] {
* def index = Action {
* ...
* }
* def create(id: Long) = Action {
* ...
* }
 
* ...
* }
* object MyResource extends ManagedResourceRouter[Long, MyResource]
* }}}
*
* Then from a routes file can be used like so:
*
* {{{
* -> /myresource controllers.MyResource
* }}}
*
* @param name A custom name for the resource controller. This is used by the Javascript router.
* @param idBindable The bindable for converting the id to the expected type.
*/
class ManagedResourceRouter[T, R <: ResourceController[T]](name: Option[String] = None)(implicit idBindable: PathBindable[T], ct: ClassTag[R]) extends ResourceRouter[T] {
 
private def invoke(action: R => EssentialAction) = {
Play.maybeApplication.map { app =>
action(app.global.getControllerInstance(ct.runtimeClass.asInstanceOf[Class[R]]))
} getOrElse {
Action(Results.InternalServerError("No application"))
}
}
 
def index = invoke(_.index)
 
def newScreen = invoke(_.newScreen)
 
def create = invoke(_.create)
 
def show(id: T) = invoke(_.show(id))
 
def edit(id: T) = invoke(_.edit(id))
 
def update(id: T) = invoke(_.update(id))
 
def destroy(id: T) = invoke(_.destroy(id))
}
 
class ResourceReverseRouter[T](path: String)(implicit idBindable: PathBindable[T]) {
private def route(method: String, p: String = "") = Call("GET", path + p)
private def routeWithId(method: String, id: T, p: String = "") = Call(method, path + "/" + idBindable.unbind("id", id) + p)
 
def index() = route("GET")
def newScreen() = route("GET", "/new")
def create() = route("POST")
def show(id: T) = routeWithId("GET", id)
def edit(id: T) = routeWithId ("GET", id, "/edit")
def update(id: T) = routeWithId("PUT", id)
def destroy(id: T) = routeWithId("DELETE", id)
}
 
class ResourceJavascriptReverseRouter[T](name: String, path: String)(implicit idBindable: PathBindable[T]) {
private def route(action: String, method: String, p: String = "") = JavascriptReverseRoute(name + "." + action,
s"""
function() {
return _wA({method: "$method", url: "$path$p"});
}
"""
)
private def routeWithId(action: String, method: String, p: String = "") = JavascriptReverseRoute(name + "." + action,
s"""
function(id) {
return _wA({method: "$method",
url: "$path/" + (${idBindable.javascriptUnbind})("id", encodeURIComponent(id)) + "$p"});
}
"""
)
 
def index = route("index", "GET")
def newScreen = route("newScreen", "GET", "/new")
def create = route("create", "POST")
def show = routeWithId("show", "GET")
def edit = routeWithId("edit", "GET", "/edit")
def update = routeWithId("update", "PUT")
def destroy = routeWithId("destroy", "DELETE")
 
def all = Seq(index, newScreen, create, show, edit, update, destroy)
}
routes
1 2 3 4 5
-> /static/resource controllers.MyResource
POST /static/resource/:id/custom controllers.MyResource.custom(id: Long)
 
-> /managed/resource controllers.MyManagedResource
POST /managed/resource/:id/custom @controllers.MyManagedResource.custom(id)

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.