• Stars
    star
    126
  • Rank 284,543 (Top 6 %)
  • Language
    Scala
  • License
    MIT License
  • Created almost 13 years ago
  • Updated about 11 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Better router for Play Framework 2.x

Better router for Play Framework 2.0

The "why" and "how" - http://codetunes.com/2012/scala-dsl-tutorial-writing-web-framework-router

Installation

Add play-navigator to your project/Build.scala file

val appDependencies = Seq(
  "eu.teamon" %% "play-navigator" % "0.5.0"
)

val main = PlayProject(appName, appVersion, appDependencies, mainLang = SCALA).settings(
  resolvers += "teamon.eu repo" at "http://repo.teamon.eu"
)

From 0.5.0 only Play >= 2.2.0 is supported. Use 0.4.0 if you need to support Play < 2.2.0.

Delete conf/routes file (optional since 0.3.0, you can use both)

Create new file PROJECT_ROOT/app/controllers/nav.scala:

package controllers

import play.navigator._

object nav extends PlayNavigator {
    // Your routes definition (see below)
}

Your app/Global.scala should look like this

import play.api._
import play.api.mvc._

object Global extends GlobalSettings {

  override def onRouteRequest(request: RequestHeader) = {
    controllers.nav.onRouteRequest(request) // handle requests with play-navigator

    // optionally you can fallback to standard Play routes with
    // controllers.nav.onRouteRequest(request) orElse super.onRouteRequest(request)
  }

  override def onHandlerNotFound(request: RequestHeader) = {
    // display 404 page with routes documentation
    val result = controllers.nav.onHandlerNotFound(request)
    // Play < 2.2.0
    result 
    // Play >= 2.2.0
    Future.successful(result)
  }

}

Routes definition

// Basic. Remember to add '_' after parameterless functions.
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

// redirect
GET on "redirect-me" to redirect("http://google.com")

// assets
val assets = GET on "assets" / ** to { s: String => Assets.at(path="/public", s) }

Application and Todos controllers used in example

// app/controllers/Application.scala
package controllers

import play.api.mvc._

object Application extends Controller {

  def index(): Action[_] = Action {
    Ok("Applcation.index => " + routes.index())
  }

  def about(): Action[_] = Action {
    Ok("Application.about => " + routes.about() + " or " + routes.api.v2.about())
  }

  def show(id: Int): Action[_] = Action {
    Ok("Application.show(%d) => %s" format (id, routes.show(id)))
  }

  def bar(f: Float, b: Boolean, s: String): Action[_] = Action {
    Ok("Application.bar(%f, %b, %s) => %s" format (f, b, s, routes.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 =>
    val in = Iteratee.foreach[String](println).mapDone { _ =>
      println("Disconnected")
    }

    val out = Enumerator("Hello!")

    (in, out)
  }
}


// app/controllers/Todos.scala
package controllers

import play.api._
import play.api.mvc._
import navigator._

object Todos extends Controller with PlayResourcesController[Int] {
  def index() = Action { Ok("Todos.index => %s" format nav.todos.index()) }
  def `new`() = Action { Ok("Todos.new => %s" format nav.todos.`new`()) }
  def create() = Action { Ok("Todos.create => %s" format nav.todos.create()) }
  def show(id: Int) = Action { Ok("Todos.show(%d) => %s" format (id, nav.todos.show(id))) }
  def edit(id: Int) = Action { Ok("Todos.edit(%d) => %s" format (id, nav.todos.edit(id))) }
  def update(id: Int) = Action { Ok("Todos.update(%d) => %s" format (id, nav.todos.update(id))) }
  def delete(id: Int) = Action { Ok("Todos.delete(%d) => %s" format (id, nav.todos.delete(id))) }
}

Mountable routers

case class FirstModule(parent: PlayNavigator) extends PlayModule(parent) with Controller {
  val home = GET on root to first.Application.index _
  val foobar = GET on "foo" / "bar" / * to first.Application.foo
}

case class SecondModule(parent: PlayNavigator) extends PlayModule(parent) with Controller {
  val home = GET on root to (() => second.Application.index)
  val foobar = GET on "foo" / "bar" / * to second.Application.foo
}

// Main router
object nav extends PlayNavigator {
    val first = "first" --> FirstModule
    val second = "second" / "module" --> SecondModule
}

Generated routes:

/first
/first/foo/bar/*
/second/module
/second/module/foo/bar/*

and reverse routing:

nav.first.home() // => "/first"
nav.first.foo(3) // => "/first/foo/bar/3"
nav.second.home() // => "/second/module"
nav.second.foo(3) // => "/second/module/foo/bar/3"

More Repositories

1

tesla

The flexible HTTP client library for Elixir, with support for middleware and multiple adapters.
Elixir
1,599
star
2

akka.js

akka actors for java script
Scala
88
star
3

headless

Headless UI Components for Phoenix
Elixir
26
star
4

hclq

HCL to JSON with jq passthrough
Go
18
star
5

play-airbrake

**NOT MAINTAINED** Airbrake.io notifier for Play 2.0
Scala
16
star
6

ecto_airtable

Elixir Ecto adapter for Airtable
Elixir
15
star
7

typonf

Typed Runtime configuration for Elixir/Erlang
Elixir
14
star
8

rsvg

Render SVG into PNG in Elixir
Elixir
13
star
9

play-scalaz

Scalaz integration for Play 2.0
Scala
12
star
10

minirails

Smallest Rails (and other) Apps
Ruby
10
star
11

plan-pwr.pl

Generator planu PWR
Ruby
10
star
12

merb-i18n

r18n binding for merb
Ruby
8
star
13

merb-flash

Rails-like flash :message replacement
Ruby
6
star
14

merb-colorful-logger

Adding some color to merb console
Ruby
6
star
15

resvg

Render SVG to PNG in Elixir using resvg
Elixir
5
star
16

sickle

Sickle is dead simple library for building complex command line tools
Ruby
5
star
17

optparse

Scala type safe command line options parser built using
Scala
5
star
18

asdf-pre

Speed up asdf installs on alpine in docker using precompiled binaries
Shell
4
star
19

avr

Misc stuff for AVR microcontrollers
C++
4
star
20

jogger

Edytor szablonรณw Joggera
Ruby
4
star
21

synergio

Cocoa application with QuartzComposer-like interface
JavaScript
3
star
22

sumo

Sumo robot firmware
C++
3
star
23

SumoDebugger

Mac OSX application for monitoring MiniSumo robot
Objective-C
3
star
24

egy

Sumobot
C++
3
star
25

tesla-openapi-examples

Elixir
3
star
26

pwr

DEPRECATED: Second version of plan-pwr. See
JavaScript
3
star
27

tweets

Elixir
3
star
28

scalajars.org

JavaScript
3
star
29

ffi-opengl-dsl

Make fun with OpenGL in Ruby!
Ruby
3
star
30

devise_active_resource

An extension for Devise to use ActiveResource as a gateway to any backend storage
Ruby
2
star
31

piast

C++
2
star
32

klp

KLP Printer
Ruby
2
star
33

teamon.eu

Source code for my homepage
Ruby
2
star
34

sumocpp

avr sumo library
C++
2
star
35

yeti

Scala
2
star
36

unfiltered-rest

REST communication between unfiltered and ActiveResource
Ruby
2
star
37

pokapoka

Just show your README with github styling in browser
Ruby
2
star
38

algo

Ruby
1
star
39

BierCalc

Java
1
star
40

spectest

Ruby
1
star
41

elixir-test-doubles

Elixir
1
star
42

forms

Scala
1
star
43

naja

Scala
1
star
44

sandbox

Elixir sandbox
Elixir
1
star
45

pwr_ooold

DEPRECATED: First version of plan-pwr, see
JavaScript
1
star
46

tools

some random tools
Python
1
star
47

merb-ext

Merb extensions
Ruby
1
star
48

merb-scala

Ruby
1
star
49

winko3

Scala
1
star
50

akka-api

Scala
1
star
51

lift-widgets-tabs

jQuery Tabs Widget for Lift framework
JavaScript
1
star
52

sdizo_old

C++
1
star
53

lift-crud

Scala
1
star
54

volley

Java
1
star
55

testrepo

1
star
56

winko2

Java
1
star
57

programy

C++
1
star
58

auth

Ruby
1
star
59

dbbench

Scala
1
star
60

hclfmt

Makefile
1
star
61

pi

1
star
62

kick

Elixir
1
star
63

failure

Projekt na niezawodnosc
Ruby
1
star
64

bicod

Binary compare databases
Haskell
1
star
65

winko

Wielofunkcyjny Interaktywny Nablatowy Kontroler Oprogramowania
Scala
1
star
66

snippets

Some snippets (too much for gist...)
Ruby
1
star
67

SumoSim

(Mini)Sumo simulator
Scala
1
star
68

ts

Ruby
1
star