Created
February 19, 2012 21:51
-
-
Save teamon/1866044 to your computer and use it in GitHub Desktop.
Play router
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1. Delete conf/routes file | |
2. Put PlayNavigator.scala and Routes.scala in app/ directory | |
3. Put Application.scala and Todos.scala in app/controllers/ directory | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package controllers | |
// import play.api._ | |
import play.api.mvc._ | |
object Application extends Controller { | |
def index: Action[_] = Action { | |
Ok("Applcation.index => " + Routing.index()) | |
} | |
def about: Action[_] = Action { | |
Ok("Application.about => " + Routing.about() + " or " + Routing.api.v2.about()) | |
} | |
def show(id: Int): Action[_] = Action { | |
Ok("Application.show(%d) => %s" format (id, Routing.show(id))) | |
} | |
def bar(f: Float, b: Boolean, s: String): Action[_] = Action { | |
Ok("Application.bar(%f, %b, %s) => %s" format (f, b, s, Routing.bar(f,b,s))) | |
} | |
def long(path: String) = Action { | |
Ok("Application.long(%s)" format path) | |
} | |
def extJson(id: Int) = Action { Ok("Application.extJson(%d)" format id) } | |
def extXml(id: String) = Action { Ok("Application.extXml(%s)" format id) } | |
import play.api.libs.iteratee._ | |
def ws = WebSocket.using[String] { request => | |
// Log events to the console | |
val in = Iteratee.foreach[String](println).mapDone { _ => | |
println("Disconnected") | |
} | |
// Send a single 'Hello!' message | |
val out = Enumerator("Hello!") | |
(in, out) | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
GET / | |
GET /index | |
GET /about | |
POST /foo | |
GET /show/[Int] | |
GET /ws | |
GET /bar/[Float]/[Boolean]/blah/[java.lang.String] | |
GET /todos | |
GET /todos/new | |
POST /todos | |
GET /todos/[Int] | |
GET /todos/[Int]/edit | |
PUT /todos/[Int] | |
DELETE /todos/[Int] | |
GET /long/** | |
GET /ext/[Int].json | |
GET /ext/[String].xml | |
GET /api/v1/index | |
GET /api/v2/about | |
GET /showalt/[Int] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import play.core.Router | |
import navigator.PlayNavigator | |
trait RoutesDefinition extends Router.Routes with PlayNavigator { | |
import controllers._ | |
// Basic | |
val home = GET on root to Application.index _ | |
val index = GET on "index" to Application.index _ | |
val about = GET on "about" to Application.about _ | |
val foo = POST on "foo" to Application.about _ | |
val show = GET on "show" / * to Application.show | |
val ws = GET on "ws" to Application.ws _ | |
val bar = GET on "bar" / * / * / "blah" / * to Application.bar | |
// Catches /long/a/b/c/.../z | |
var long = GET on "long" / ** to Application.long | |
// Require extension: /ext/{param}.{ext} | |
GET on "ext" / * as "json" to Application.extJson | |
GET on "ext" / * as "xml" to Application.extXml | |
// REST routes | |
val todos = resources("todos", Todos) | |
// Namespace ... | |
namespace("api"){ | |
namespace("v1"){ | |
GET on "index" to Application.index _ | |
} | |
} | |
// ... or with reverse routing support | |
val api = new Namespace("api"){ | |
val v2 = new Namespace("v2"){ | |
val about = GET on "about" to Application.about _ | |
} | |
} | |
// and back to top-level namespace | |
GET on "showalt" / * to Application.show | |
} | |
package controllers { | |
object Routing extends RoutesDefinition | |
} | |
object Routes extends RoutesDefinition |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package controllers | |
import play.api._ | |
import play.api.mvc._ | |
import navigator._ | |
object Todos extends Controller with PlayResources[Int] { | |
def index = Action { Ok("Todos.index => %s" format Routing.todos.index()) } | |
def `new` = Action { Ok("Todos.new => %s" format Routing.todos.`new`()) } | |
def create = Action { Ok("Todos.create => %s" format Routing.todos.create()) } | |
def show(id: Int) = Action { Ok("Todos.show(%d) => %s" format (id, Routing.todos.show(id))) } | |
def edit(id: Int) = Action { Ok("Todos.edit(%d) => %s" format (id, Routing.todos.edit(id))) } | |
def update(id: Int) = Action { Ok("Todos.update(%d) => %s" format (id, Routing.todos.update(id))) } | |
def delete(id: Int) = Action { Ok("Todos.delete(%d) => %s" format (id, Routing.todos.delete(id))) } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment