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 { ...

Get Scala for Data Science now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.