Parsed documentation:
View on GitHub
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 MyAppWeb.Router do
      use Phoenix.Router

      get "/pages/:page", PageController, :show

The `get/3` macro above accepts a request of format `"/pages/VALUE"` and
dispatches it to the show action in the `PageController`.

Routes can also match glob-like patterns, routing any path with a common
base to the same controller. For example:

    get "/dynamic*anything", DynamicController, :show

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:


will generate the following named helper:

    MyAppWeb.Router.Helpers.page_path(conn_or_endpoint, :show, "hello")

    MyAppWeb.Router.Helpers.page_path(conn_or_endpoint, :show, "hello", some: "query")

    MyAppWeb.Router.Helpers.page_url(conn_or_endpoint, :show, "hello")

    MyAppWeb.Router.Helpers.page_url(conn_or_endpoint, :show, "hello", some: "query")

If the route contains glob-like patterns, parameters for those have to be given as

    MyAppWeb.Router.Helpers.dynamic_path(conn_or_endpoint, :show, ["dynamic", "something"])

The URL generated in the named URL helpers is based on the configuration for
`:url`, `:http` and `:https`. However, if for some reason you need to manually
control the URL generation, the url helpers also allow you to pass in a `URI`

    uri = %URI{scheme: "https", host: ""}
    MyAppWeb.Router.Helpers.page_url(uri, :show, "hello")

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:

    MyAppWeb.Router.Helpers.special_page_path(conn, :show, "hello")

## Scopes and Resources

It is very common in Phoenix applications to namespace all of your
routes under the application scope:

    scope "/", MyAppWeb do
      get "/pages/:id", PageController, :show

The route above will dispatch to `MyAppWeb.PageController`. This syntax
is not only convenient for developers, since we don't have to repeat
the `MyAppWeb.` prefix on all routes, but it also allows Phoenix to put
less pressure on the Elixir compiler. If instead we had written:

    get "/pages/:id", MyAppWeb.PageController, :show

The Elixir compiler would infer that the router depends directly on
`MyAppWeb.PageController`, which is not true. By using scopes, Phoenix
can properly hint to the Elixir compiler the controller is not an
actual dependency of the router. This provides more efficient
compilation times.

Scopes allow us to scope on any path or even on the helper name:

    scope "/api/v1", MyAppWeb, as: :api_v1 do

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 MyAppWeb.Router do
      use Phoenix.Router

      resources "/pages", PageController, only: [:show]
      resources "/users", UserController, except: [:delete]

Finally, Phoenix ships with a `mix phx.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 phx.routes
    page_path  GET    /pages/:id
    user_path  GET    /users           UserController.index/2
    user_path  GET    /users/:id/edit  UserController.edit/2
    user_path  GET    /users/new
    user_path  GET    /users/:id
    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 phx.routes MyAppWeb.Router

Check `scope/2` and `resources/4` for more information.

## Pipelines and plugs

Once a request arrives at 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 MyAppWeb.Router do
      use Phoenix.Router

      pipeline :browser do
        plug :fetch_session
        plug :accepts, ["html"]

      scope "/" do
        pipe_through :browser

        # browser related routes and resources

`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.
No suggestions.