Current section

Files

Jump to
phoenix lib phoenix router.ex
Raw

lib/phoenix/router.ex

defmodule Phoenix.Router do
defmodule NoRouteError do
@moduledoc """
Exception raised when no route is found.
"""
defexception plug_status: 404, message: "no route found", conn: nil, router: nil
def exception(opts) do
conn = Keyword.fetch!(opts, :conn)
router = Keyword.fetch!(opts, :router)
path = "/" <> Enum.join(conn.path_info, "/")
%NoRouteError{message: "no route found for #{conn.method} #{path} (#{inspect router})",
conn: conn, router: router}
end
end
@moduledoc """
Defines a Phoenix router.
The router provides a set of macros for generating routes
that dispatch to specific controllers and actions. Those
macros are named after HTTP verbs. For example:
defmodule MyApp.Router do
use Phoenix.Router
get "/pages/:page", PageController, :show
end
The `get/3` macro above accepts a request of format "/pages/VALUE" and
dispatches it to the show action in the `PageController`.
Phoenix's router is extremely efficient, as it relies on Elixir
pattern matching for matching routes and serving requests.
### Helpers
Phoenix automatically generates a module `Helpers` inside your router
which contains named helpers to help developers generate and keep
their routes up to date.
Helpers are automatically generated based on the controller name.
For example, the route:
get "/pages/:page", PageController, :show
will generate a named helper:
MyApp.Router.Helpers.page_path(:show, "hello")
"/pages/hello"
MyApp.Router.Helpers.page_path(:show, "hello", some: "query")
"/pages/hello?some=query"
The named helper can also be customized with the `:as` option. Given
the route:
get "/pages/:page", PageController, :show, as: :special_page
the named helper will be:
MyApp.Router.Helpers.special_page_path(:show, "hello")
"/pages/hello"
### Scopes and Resources
The router also supports scoping of routes:
scope "/api/v1", as: :api_v1 do
get "/pages/:id", PageController, :show
end
For example, the route above will match on the path `"/api/v1/pages/:id"
and the named route will be `api_v1_page_path`, as expected from the
values given to `scope/2` option.
Phoenix also provides a `resources/4` macro that allows developers
to generate "RESTful" routes to a given resource:
defmodule MyApp.Router do
use Phoenix.Router
pipe_through :browser
resources "/pages", PageController, only: [:show]
resources "/users", UserController, except: [:destroy]
end
Finally, Phoenix ships with a `mix phoenix.routes` task that nicely
formats all routes in a given router. We can use it to verify all
routes included in the router above:
$ mix phoenix.routes
page_path GET /pages/:id PageController.show/2
user_path GET /users UserController.index/2
user_path GET /users/:id/edit UserController.edit/2
user_path GET /users/new UserController.new/2
user_path GET /users/:id UserController.show/2
user_path POST /users UserController.create/2
user_path PATCH /users/:id UserController.update/2
PUT /users/:id UserController.update/2
One can also pass a router explicitly as an argument to the task:
$ mix phoenix.routes MyApp.Router
Check `scope/2` and `resources/4` for more information.
## Pipelines and plugs
Once a request arrives to the Phoenix router, it performs
a series of transformations through pipelines until the
request is dispatched to a desired end-point.
Such transformations are defined via plugs, as defined
in the [Plug](http://github.com/elixir-lang/plug) specification.
Once a pipeline is defined, it can be piped through per scope.
For example:
defmodule MyApp.Router do
use Phoenix.Router
pipeline :browser do
plug :fetch_session
plug :accepts, ~w(html json)
end
scope "/" do
pipe_through :browser
# browser related routes and resources
end
end
`Phoenix.Router` imports functions from both `Plug.Conn` and `Phoenix.Controller`
to help define plugs. In the example above, `fetch_session/2`
comes from `Plug.Conn` while `accepts/2` comes from `Phoenix.Controller`.
Note that router pipelines are only invoked after a route is found.
No plug is invoked in case no matches were found.
"""
alias Phoenix.Router.Resource
alias Phoenix.Router.Scope
@http_methods [:get, :post, :put, :patch, :delete, :options, :connect, :trace, :head]
@doc false
defmacro __using__(_) do
quote do
unquote(prelude())
unquote(plug())
end
end
defp prelude() do
quote do
@before_compile Phoenix.Router
Module.register_attribute __MODULE__, :phoenix_routes, accumulate: true
import Phoenix.Router
import Plug.Conn
import Phoenix.Controller
# Set up initial scope
@phoenix_pipeline nil
Phoenix.Router.Scope.init(__MODULE__)
end
end
defp plug() do
{conn, pipeline} =
[:dispatch, :match]
|> Enum.map(&{&1, [], true})
|> Plug.Builder.compile()
call =
quote do
unquote(conn) =
update_in unquote(conn).private,
&(&1 |> Map.put(:phoenix_router, __MODULE__)
|> Map.put(:phoenix_pipelines, []))
unquote(pipeline)
end
quote location: :keep do
@behaviour Plug
@doc """
Callback required by Plug that initializes the router
for serving web requests.
"""
def init(opts) do
opts
end
@doc """
Callback invoked by Plug on every request.
"""
def call(unquote(conn), opts) do
unquote(call)
end
defp match(conn, []) do
match(conn, conn.method, conn.path_info, conn.host)
end
defp dispatch(conn, []) do
conn.private.phoenix_route.(conn)
end
defoverridable [init: 1, call: 2]
end
end
@doc false
defmacro __before_compile__(env) do
routes = env.module |> Module.get_attribute(:phoenix_routes) |> Enum.reverse
Phoenix.Router.Helpers.define(env, routes)
quote do
@doc false
def __routes__ do
unquote(Macro.escape(routes))
end
defp match(conn, _method, _path_info, _host) do
raise NoRouteError, conn: conn, router: __MODULE__
end
end
end
for verb <- @http_methods do
method = verb |> to_string |> String.upcase
@doc """
Generates a route to handle a #{verb} request to the given path.
"""
defmacro unquote(verb)(path, controller, action, options \\ []) do
add_route(unquote(method), path, controller, action, options)
end
end
defp add_route(verb, path, controller, action, options) do
quote bind_quoted: binding() do
route = Scope.route(__MODULE__, verb, path, controller, action, options)
parts = {:%{}, [], route.binding}
@phoenix_routes route
defp match(var!(conn), unquote(route.verb), unquote(route.path_segments),
unquote(route.host_segments)) do
var!(conn) =
Plug.Conn.put_private(var!(conn), :phoenix_route, fn conn ->
conn = update_in(conn.params, &Map.merge(&1, unquote(parts)))
opts = unquote(route.controller).init(unquote(route.action))
unquote(route.controller).call(conn, opts)
end)
|> Plug.Conn.put_private(:phoenix_pipelines, unquote(route.pipe_through))
unquote(route.pipe_segments)
end
end
end
@doc """
Defines a plug pipeline.
Pipelines are defined at the router root and can be used
from any scope.
## Examples
pipeline :api do
plug :token_authentication
plug :dispatch
end
A scope may then use this pipeline as:
scope "/" do
pipe_through :api
end
Every time `pipe_through/1` is called, the new pipelines
are appended to the ones previously given.
"""
defmacro pipeline(plug, do: block) do
block =
quote do
@phoenix_pipeline []
unquote(block)
end
compiler =
quote bind_quoted: [plug: plug] do
Scope.pipeline(__MODULE__, plug)
{conn, body} = Plug.Builder.compile(@phoenix_pipeline)
defp unquote(plug)(unquote(conn), _), do: unquote(body)
@phoenix_pipeline nil
end
[block, compiler]
end
@doc """
Defines a plug inside a pipeline.
See `pipeline/2` for more information.
"""
defmacro plug(plug, opts \\ []) do
quote do
if pipeline = @phoenix_pipeline do
@phoenix_pipeline [{unquote(plug), unquote(opts), true}|pipeline]
else
raise "cannot define plug at the router level, plug must be defined inside a pipeline"
end
end
end
@doc """
Defines a pipeline to send the connection through.
See `pipeline/2` for more information.
"""
defmacro pipe_through(pipes) do
quote do
if pipeline = @phoenix_pipeline do
raise "cannot pipe_through inside a pipeline"
else
Scope.pipe_through(__MODULE__, unquote(pipes))
end
end
end
@doc """
Defines "RESTful" routes for a resource.
The given definition:
resources "/users", UserController
will include routes to the following actions:
* `GET /users` => `:index`
* `GET /users/new` => `:new`
* `POST /users` => `:create`
* `GET /users/:id` => `:show`
* `GET /users/:id/edit` => `:edit`
* `PATCH /users/:id` => `:update`
* `PUT /users/:id` => `:update`
* `DELETE /users/:id` => `:destroy`
## Options
This macro accepts a set of options:
* `:only` - a list of actions to generate routes for, for example: `[:show, :edit]`
* `:except` - a list of actions to exclude generated routes from, for example: `[:destroy]`
* `:param` - the name of the paramter for this resource, defaults to `"id"`
* `:name` - the prefix for this resource. This is used for the named helper
and as the prefix for the parameter in nested resources. The default value
is automatically derived from the controller name, i.e. `UserController` will
have name `"user"`
* `:as` - configures the named helper exclusively
"""
defmacro resources(path, controller, opts, do: nested_context) do
add_resources path, controller, opts, do: nested_context
end
@doc """
See `resources/4`.
"""
defmacro resources(path, controller, do: nested_context) do
add_resources path, controller, [], do: nested_context
end
defmacro resources(path, controller, opts) do
add_resources path, controller, opts, do: nil
end
@doc """
See `resources/4`.
"""
defmacro resources(path, controller) do
add_resources path, controller, [], do: nil
end
defp add_resources(path, controller, options, do: context) do
quote do
# TODO: Support :alias as option (which is passed to scope)
resource = Resource.build(unquote(path), unquote(controller), unquote(options))
parm = resource.param
path = resource.path
ctrl = resource.controller
opts = [as: resource.as]
Enum.each resource.actions, fn action ->
case action do
:index -> get "#{path}", ctrl, :index, opts
:show -> get "#{path}/:#{parm}", ctrl, :show, opts
:new -> get "#{path}/new", ctrl, :new, opts
:edit -> get "#{path}/:#{parm}/edit", ctrl, :edit, opts
:create -> post "#{path}", ctrl, :create, opts
:destroy -> delete "#{path}/:#{parm}", ctrl, :destroy, opts
:update ->
patch "#{path}/:#{parm}", ctrl, :update, opts
put "#{path}/:#{parm}", ctrl, :update, as: nil
end
end
scope resource.member do
unquote(context)
end
end
end
@doc """
Defines a scope in which routes can be nested.
## Examples
scope "/api/v1", as: :api_v1, alias: API.V1 do
get "/pages/:id", PageController, :show
end
The generated route above will match on the path `"/api/v1/pages/:id"
and will dispatch to `:show` action in `API.V1.PageController`. A named
helper `api_v1_page_path` will also be generated.
## Options
The supported options are:
* `:path` - a string containing the path scope
* `:as` - a string or atom containing the named helper scope
* `:alias` - an alias (atom) containing the controller scope
* `:host` - a string containing the host scope, or prefix host scope, ie
`"foo.bar.com"`, `"foo."`
"""
defmacro scope(options, do: context) do
do_scope(options, context)
end
@doc """
Define a scope with the given path.
This function is a shortcut for:
scope path: path do
...
end
"""
defmacro scope(path, options, do: context) do
options = quote do
path = unquote(path)
case unquote(options) do
alias when is_atom(alias) -> [path: path, alias: alias]
options when is_list(options) -> Keyword.put(options, :path, path)
end
end
do_scope(options, context)
end
@doc """
Define a scope with the given path and alias.
This function is a shortcut for:
scope path: path, alias: alias do
...
end
"""
defmacro scope(path, alias, options, do: context) do
options = quote do
unquote(options)
|> Keyword.put(:path, unquote(path))
|> Keyword.put(:alias, unquote(alias))
end
do_scope(options, context)
end
defp do_scope(options, context) do
quote do
Scope.push(__MODULE__, unquote(options))
try do
unquote(context)
after
Scope.pop(__MODULE__)
end
end
end
end