Product Catalog Application using Play and Scala Part 6 – Route And Controller

Wed, Aug 6, 2014

Play Programming #Play #programming #scala

In Play framework, we will be using controllers to handle user request and route configurations to forward request to controller.

Route configurations are stored in conf/routes file. Here is what in the configuration file.

# Routes
# This file defines all application routes (Higher priority routes first)
# ~~~~

# Home page
GET     /                           controllers.Application.index

# Map static resources from the /public folder to the /assets URL path
GET     /assets/*file               controllers.Assets.at(path="/public", file)

There are 2 entries in the configuration file. The first entry indicates the homepage. The “GET” indicates the HTTP GET method, and the “/” means the path, and the last part “controllers.Application.index” means “index” method in “Application” object in “controllers” package. And for the second entry it is meant for static resources, for example CSS file.

Let’s create an empty controller with unimplemented methods for user to list, create, delete, show and update Product and Category records. Every controller object will be extending Controller traits.

package controllers

import models._
import play.api._
import play.api.mvc._
import play.api.data._
import play.api.data.validation.Constraints._
import views._
import models._
import play.api.Logger

object Products extends Controller {
    def list = TODO
  
    def add = TODO

    def submit = TODO
  
    def delete(id: Long) = TODO

    def edit(id: Long) = TODO
}

At the moment every method will return a TODO result, when we trying to access the page it will show the message “Action not implemented yet.”. And in the route configuration file we will add the following entries.

# Products 
GET     /products                 controllers.Products.list
GET     /products/add             controllers.Products.add
POST    /products/add             controllers.Products.submit
GET     /products/:id/edit        controllers.Products.edit(id: Long)
GET     /products/:id/delete      controllers.Products.delete(id: Long)

The new entries are slightly different, for example “/products/:id/edit”, the part “:id” acts like a parameter placeholder, and we will use this param and pass it to the method.

Now we run the web server.

[product-catalog] $ run 8000
--- (Running the application from SBT, auto-reloading is enabled) ---

[info] play - Listening for HTTP on /0:0:0:0:0:0:0:0:8000

(Server started, use Ctrl+D to stop and go back to the console...)

And we open browser and go to http://localhost:8000/products , this is what we will be seeing.

TODO result.

Let’s implement our first method, list.

def list = Action{
  Ok(Product.list.map(_.toString).mkString(","))
}

Methods in controller that handle incoming requests are expected to return an Action value. In the previous code, we are listing all the products in the table and convert the list of products to a string and return to user. And when we open the url http://localhost:8000/products we will see something like this(assume we have some sample data in Products table).

Products - Product(1,Product A(updated),3,Category A)

You can manually insert some data into Products table at the moment to see how this goes.

For the rest of the methods, we will be implementing them in the next article when we look at form and view template.