View Source Orbit.Router (Orbit v0.2.0)

Sort out incoming requests.

usage

Usage

use Orbit.Router injects the following into the module:

import Orbit.Router

def call(req, arg)

example

Example

defmodule MyApp.Router do
  use Orbit.Router

  pipe {Orbit.Controller, :push_layout}, {MyApp.LayoutView, :main}
  pipe MyApp.SetCurrentUser

  route "/static/*path", Orbit.Static, from: "priv/static"

  group do
    pipe MyApp.RequireCurrentUser

    route "/messages", MyApp.MessageController, :index
    route "/messages/:id", MyApp.MessageController, :show
  end
end

Link to this section Summary

Functions

Defines a group of routes with a shared pipeline.

Defines a pipe through which requests are processed.

Defines a route that sends a request to a designated pipe.

Link to this section Functions

Defines a group of routes with a shared pipeline.

Groups have their own pipelines that append any existing pipes from parent groups, or from the router. Groups can be nested.

example

Example

pipe SetCurrentUser

route "/", HomeController, :show
# ...more routes for all users...

group do
  pipe RequireUser

  route "/profile", ProfileController, :show
  # ...more routes for authenticated users...

  group do
    pipe RequireAdminRole

    route "/admin/users", UserController, :index
    # ...more routes for authenticated admin users...
  end
end
Link to this macro

pipe(pipe, arg \\ [])

View Source (macro)

Defines a pipe through which requests are processed.

The pipe argument may be either:

  • a module that implements Orbit.Pipe
  • a function capture of a 2-arity function
  • a {module, function} tuple that points to a 2-arity public function in a module

If the pipe halts the request, the router does not process any further pipes or route matches.

Pipelines (sets of pipes) may be constructed by defining pipes inside group/1 blocks.

Link to this macro

route(path, pipe, arg \\ [])

View Source (macro)

Defines a route that sends a request to a designated pipe.

Path segments can contain parameters which are merged into the params field of the request. A wildcard parameter can exist at the very end of a path match.

route "/users/:id/edit", UserController, :edit # => %{"id" => "123"}
route "/posts/*slug", PostController, :show # => %{"slug" => "favorite/cat/pictures"}

The pipe argument may be either:

  • a module that implements Orbit.Pipe
  • a function capture of a 2-arity function
  • a {module, function} tuple that points to a 2-arity public function in a module

If no route matches the request path, the router responds with a :not_found status.