Dynamic routing
Routing, as we saw, is the mapping of HTTP requests to Scala handlers. Routes are stored in conf/routes
. A route is defined by an HTTP verb, followed by the end-point, followed by a Scala function:
// verb // end-point // Scala handler GET / controllers.Application.index
We learnt to add new routes by just adding lines to the routes
file. We are not limited to static routes, however. The Play framework lets us include wild cards in routes. The value of the wild card can be passed as an argument to the controller. To see how this works, let's create a controller that takes the name of a person as argument. In the Application
object in app.controllers
, add:
// app/controllers/Application.scala class Application extends Controller { ... def hello(name:String) = Action { Ok(s"hello, $name") } }
We can now define a route handled by this controller:
// conf/routes
GET /hello/:name controllers.Application...