Long dynamic routes in play framework 2

jrook

I am developing an app for showing different aspects of cars. The app has a tree-like structure like this:

Country>Manufacturer>Model>Car_subsystem>Part

I want this structure to be reflected in browser's address bar:

http://localhost:9000/Germany/BMW/X6/Body/doors

Currently, I do it with play framework's dynamic routing as below:

GET /:Country    controllers.Application.controllerFunction(Country : String)
GET /:Country/:Manufacturer     controllers.Application.controllerFunction(Country : String, Manufacturer : String)

etc.

This works, but I don't like passing around 5 or 6 parameters to all my controller functions just for the paths to be shown beautifully! Is there any other way?

biesior

Just use Dynamic parts spanning several / as described in routing doc

route:

GET   /Cars/*path          controllers.Application.carResolver(path) 

Action (simplest approach)

public static Result carResolver(String path) {
    Car car = Car.findByPath(path);
    return ok(carView.render(car));
}

so each car should have its field path filled with unique string ID, ie: Germany/BMW/X6, Germany/Mercedes/ASL` etc.

Of course it will be much better if you split the path arg by the slash first so you can use each part for displaying different views, 'translate' strings to real object ID etc etc.

public static Result carResolver(String path) {

    String[] parts = path.split("/");
    int partsLength = parts.length;

    String country = (partsLength > 0) ? parts[0] : null;
    String manufacturer = (partsLength > 1) ? parts[1] : null;
    String carModel = (partsLength > 2) ? parts[2] : null;
    // etc

    switch (partsLength){
        case 0: return countrySelect();
        case 1: return allManufacturersIn(country);
        case 2: return allModelsOf(manufacturer);
        case 3: return singleViewFor(carModel);
        // etc
    }

    return notFound("Didn't find anything for required path...");
}

TIP: "translating" strings to objects will require from you searching in DB by some field, so there's some advices:

  • Try to make sure that each model has a unique field for searching ie. Country should have unique name as probably you don't want to have Germany 1, Germany 2 etc.
  • This approach requires probably more time than searching by numeric ID's so try to cache somehow (mem-cache OR at least dedicated DB table) the result of resolving ie.:

    Germany/BMW/X6 = Country: 1, Manufacturer: 2, CarModel: 3, action: "singleViewFor" 
    

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to "rewrite" urls or routes with Play Framework 2

From Dev

Play framework routes

From Dev

Dynamic routes in Phoenix Framework

From Dev

Play framework 2 : How to pass object between routes, views, and controller?

From Dev

play framework - bind enum in routes

From Dev

Play Framework, REST, Routes, and Controllers

From Dev

Scala backticks in Play Framework Routes

From Dev

Domain routes in Play Framework 2.3

From Dev

Play Framework Error in Routes file

From Dev

Play Framework 2 Stage Task on Windows, "The input line is too long"

From Dev

Play Framework 2 Stage Task on Windows, "The input line is too long"

From Dev

Non-capturing wildcards in Play Framework routes

From Dev

Using POST routes parameters in Play Framework

From Dev

Iterating all Play Framework routes in Test

From Dev

split routes in multiple files for play framework 2.2

From Dev

Play Framework @routes.Assets.at Compilation Error

From Dev

Play framework - call controller in routes with Class parameter

From Dev

Play framework routes, and scala predef values

From Dev

Play Framework 2.3.4 routes redirection to subproject

From Dev

Non-capturing wildcards in Play Framework routes

From Dev

Using POST routes parameters in Play Framework

From Dev

Play framework - call controller in routes with Class parameter

From Dev

Play Framework 2.3.4 routes redirection to subproject

From Dev

Play framework dynamic template include

From Dev

Scala Play Framework - Dynamic Forms

From Dev

Play framework dynamic template include

From Dev

Objectify with Play Framework 2

From Dev

Play Framework takes too long to load with hibernate

From Dev

Play 2 - Access conf property in routes file

Related Related

HotTag

Archive