Phoenix.Router
Defines the Phoenix router.
A router is the heart of a Phoenix application. It has three main responsibilities:
It defines a plug pipeline responsible for handling upcoming requests and dispatching those requests to controllers and other plugs.
It hosts configuration for the router and related entities (like plugs).
- It provides a wrapper for starting and stopping the router in a specific web server.
We will explore those responsibilities next.
Routing
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 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
.
By default, Phoenix ships with one pipeline, called :before
,
that is always invoked before any route matches. All other
pipelines are invoked only after a specific route matches,
but before the route is dispatched to.
:before pipeline
Those are the plugs in the :before
pipeline in the order
they are defined. How each plug is configured is defined in
a later sections.
Plug.Static
- serves static assets. Since this plug comes before the router, serving of static assets is not loggedPlug.Logger
- logs incoming requestsPlug.Parsers
- parses the request body when a known parser is available. By default parsers urlencoded, multipart and json (with poison). The request body is left untouched when the request content-type cannot be parsedPlug.MethodOverride
- converts the request method toPUT
,PATCH
orDELETE
forPOST
requests with a valid_method
parameterPlug.Head
- convertsHEAD
requests toGET
requests and strips the response bodyPlug.Session
- a plug that sets up session management. Note thatfetch_session/2
must still be explicitly called before using the session as this plug just sets up how the session is fetchedPhoenix.CodeReloader
- a plug that enables code reloading for all entries in theweb
directory. It is configured directly in the Phoenix application
Customizing pipelines
You can define new pipelines at any moment with the pipeline/2
macro:
pipeline :api do
plug :token_authentication
end
And then in a scope (or at root):
pipe_through [:api]
Router configuration
All routers are configured directly in the Phoenix application environment. For example:
config :phoenix, YourApp.Router,
secret_key_base: "kjoy3o1zeidquwy1398juxzldjlksahdk3"
Phoenix configuration is split in two categories. Compile-time configuration means the configuration is read during compilation and changing it at runtime has no effect. Most of the compile-time configuration is related to pipelines and plugs.
On the other hand, runtime configuration is accessed during or
after your application is started and can be read through the
config/2
function:
YourApp.Router.config(:port)
YourApp.Router.config(:some_config, :default_value)
Compile-time
:session
- configures thePlug.Session
plug. Defaults tofalse
but can be set to a keyword list of options as defined inPlug.Session
. For example:config :phoenix, YourApp.Router, session: [store: :cookie, key: "_your_app_key"]
:parsers
- sets up the request parsers. Accepts a set of options as defined byPlug.Parsers
. If parsers are disabled, parameters won’t be explicitly fetched before matching a route and functionality dependent on parameters, like thePlug.MethodOverride
, will be disabled too. Defaults to:[pass: ["*/*"], json_decoder: Poison, parsers: [:urlencoded, :multipart, :json]]
:static
- sets up static assets serving. Accepts a set of options as defined byPlug.Static
. Defaults to:[at: "/", from: Mix.Project.config[:app]]
:debug_errors
- when true, usesPlug.Debugger
functionality for debugging failures in the application. Recomended to be set to true only in development as it allows listing of the application source code during debugging. Defaults to false.:render_errors
- a module representing a view to render templates whenever there is a failure in the application. For example, if the application crashes with a 500 error during a HTML request,render("500.html", assigns)
will be called in the view given to:render_errors
. The default view isMyApp.ErrorsView
.
Runtime
:http
- the configuration for the http server. Currently uses cowboy and accepts all options as defined byPlug.Adapters.Cowboy
. Defaults to false.:https
- the configuration for the https server. Currently uses cowboy and accepts all options as defined byPlug.Adapters.Cowboy
. Defaults to false.:secret_key_base
- a secret key used as base to generate secrets to encode cookies, session and friends. Defaults to nil as it must be set per application.:url
- configuration for generating URLs throughout the app. Accepts the host, scheme and port. Defaults to:[host: "localhost"]
Web server
Starting a router as part of a web server can be done by invoking
YourApp.Router.start/0
. Stopping the router is done with
YourApp.Router.stop/0
. The web server is configured with the
:http
and :https
options defined above.
Summary
connect(path, controller, action, options \\ []) | Generates a route to handle a connect request to the given path |
delete(path, controller, action, options \\ []) | Generates a route to handle a delete request to the given path |
get(path, controller, action, options \\ []) | Generates a route to handle a get request to the given path |
head(path, controller, action, options \\ []) | Generates a route to handle a head request to the given path |
options(path, controller, action, options \\ []) | Generates a route to handle a options request to the given path |
patch(path, controller, action, options \\ []) | Generates a route to handle a patch request to the given path |
pipe_through(pipes) | Defines a pipeline to send the connection through |
pipeline(plug, list2) | Defines a plug pipeline |
plug(plug, opts \\ []) | Defines a plug inside a pipeline |
post(path, controller, action, options \\ []) | Generates a route to handle a post request to the given path |
put(path, controller, action, options \\ []) | Generates a route to handle a put request to the given path |
resources(path, controller) | See |
resources(path, controller, opts) | See |
resources(path, controller, opts, list4) | Defines “RESTful” endpoints for a resource |
scope(options, list2) | Defines a scope in which routes can be nested |
scope(path, options, list3) | Define a scope with the given path |
scope(path, alias, options, list4) | Define a scope with the given path and alias |
trace(path, controller, action, options \\ []) | Generates a route to handle a trace request to the given path |
Macros
Generates a route to handle a connect request to the given path.
Generates a route to handle a delete request to the given path.
Generates a route to handle a get request to the given path.
Generates a route to handle a head request to the given path.
Generates a route to handle a options request to the given path.
Generates a route to handle a patch request to the given path.
Defines a pipeline to send the connection through.
See pipeline/2
for more information.
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.
Defines a plug inside a pipeline.
See pipeline/2
for more information.
Generates a route to handle a post request to the given path.
Generates a route to handle a put request to the given path.
See resources/4
.
See resources/4
.
Defines “RESTful” endpoints for a resource.
The given definition:
resources "/users", UserController
will include routes to the following actions:
GET /users
=>:index
GET /users/new
=>:new
POST /resources
=>:create
GET /resources/:id
=>:show
GET /resources/:id/edit
=>:edit
PATCH /resources/:id
=>:update
PUT /resources/:id
=>:update
DELETE /resources/: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
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."`
Define a scope with the given path.
This function is a shortcut for:
scope path: path do
...
end
Define a scope with the given path and alias.
This function is a shortcut for:
scope path: path, alias: alias do
...
end