2016-06-02 10:07:58 +00:00
|
|
|
---
|
|
|
|
title: "Phoenix: Routing"
|
|
|
|
category: Elixir
|
2017-09-04 03:20:35 +00:00
|
|
|
weight: -1
|
2016-06-02 10:07:58 +00:00
|
|
|
---
|
|
|
|
|
2017-09-04 03:20:35 +00:00
|
|
|
### Showing routes
|
|
|
|
|
2016-06-02 10:07:58 +00:00
|
|
|
```sh
|
2017-09-04 03:20:35 +00:00
|
|
|
mix phx.routes # 1.3+
|
|
|
|
mix phoenix.routes # 1.2 and below
|
2016-06-02 10:07:58 +00:00
|
|
|
```
|
|
|
|
|
2017-09-04 03:20:35 +00:00
|
|
|
See: [Mix.Tasks.Phoenix.Routes](https://hexdocs.pm/phoenix/Mix.Tasks.Phoenix.Routes.html) _(hexdocs.pm)_
|
|
|
|
|
|
|
|
### Single routes
|
2016-06-02 10:07:58 +00:00
|
|
|
|
|
|
|
```elixir
|
|
|
|
get "/", PageController, :index
|
|
|
|
```
|
|
|
|
|
2016-06-02 20:41:26 +00:00
|
|
|
Also: `put` `post` `patch` `options` `delete` `head`
|
|
|
|
|
2017-09-04 03:20:35 +00:00
|
|
|
### Resources
|
2016-06-02 10:07:58 +00:00
|
|
|
|
|
|
|
```elixir
|
|
|
|
resources "/users", UserController
|
|
|
|
resources "/users", UserController, only: [:index, :show]
|
|
|
|
resources "/users", UserController, except: [:delete]
|
2017-09-04 03:20:35 +00:00
|
|
|
```
|
2016-06-02 20:41:26 +00:00
|
|
|
|
2017-09-04 03:20:35 +00:00
|
|
|
```elixir
|
|
|
|
resources "/users", UserController,
|
2016-06-02 20:41:26 +00:00
|
|
|
as: :person # helper name (person_path)
|
|
|
|
name: :person # ...?
|
|
|
|
param: :id # name of parameter for this resource
|
2016-06-02 10:07:58 +00:00
|
|
|
```
|
|
|
|
|
2017-09-04 03:20:35 +00:00
|
|
|
Generates these routes:
|
|
|
|
|
2016-06-02 10:07:58 +00:00
|
|
|
| Method | Path | Helper |
|
|
|
|
| ---- | ---- | ---- |
|
|
|
|
| GET | `/users` | `user_path(:index)` |
|
|
|
|
| GET | `/users/new` | `user_path(:new)` |
|
|
|
|
| GET | `/users/:id` | `user_path(:show, user)` |
|
|
|
|
| GET | `/users/:id/edit` | `user_path(:edit, user)` |
|
|
|
|
| POST | `/users` | `user_path(:create, user)` |
|
|
|
|
| PATCH/PUT | `/users/:id` | `user_path(:update, user)` |
|
|
|
|
| DELETE | `/users/:id` | `user_path(:delete, user)` |
|
2017-09-04 03:20:35 +00:00
|
|
|
{: .-left-align}
|
|
|
|
|
|
|
|
See: [resources/4](https://hexdocs.pm/phoenix/Phoenix.Router.html#resources/4) _(hexdocs.pm)_
|
2016-06-02 10:07:58 +00:00
|
|
|
|
2017-09-04 03:20:35 +00:00
|
|
|
### Path helpers
|
2016-06-02 10:07:58 +00:00
|
|
|
|
|
|
|
```elixir
|
2017-09-04 03:20:35 +00:00
|
|
|
user_path(conn, :index) # → /users
|
|
|
|
user_path(conn, :show, 17) # → /users/17
|
|
|
|
user_path(conn, :show, %User{id: 17}) # → /users/17
|
|
|
|
user_path(conn, :show, 17, admin: true) # → /users/17?admin=true
|
|
|
|
```
|
2016-06-02 10:07:58 +00:00
|
|
|
|
2017-09-04 03:20:35 +00:00
|
|
|
```elixir
|
|
|
|
user_url(conn, :index) # → "http://localhost:4000/users"
|
2016-06-02 10:07:58 +00:00
|
|
|
```
|
|
|
|
|
|
|
|
```elixir
|
|
|
|
MyApp.Router.Helpers.user_path(MyApp.Endpoint, :index)
|
|
|
|
```
|
|
|
|
|
2017-09-04 03:20:35 +00:00
|
|
|
See: [Helpers](https://hexdocs.pm/phoenix/Phoenix.Router.html#module-helpers) _(hexdocs.pm)_
|
|
|
|
|
|
|
|
### Nested resources
|
2016-06-02 10:07:58 +00:00
|
|
|
|
|
|
|
```elixir
|
|
|
|
resources "/users", UserController do
|
|
|
|
resources "/posts", PostController
|
|
|
|
end
|
2017-09-04 03:20:35 +00:00
|
|
|
```
|
2016-06-02 10:07:58 +00:00
|
|
|
|
2017-09-04 03:20:35 +00:00
|
|
|
```elixir
|
|
|
|
user_post_path(:index, 17) # → /users/17/posts
|
|
|
|
user_post_path(:show, 17, 12) # → /users/17/posts/12
|
2016-06-02 10:07:58 +00:00
|
|
|
```
|
|
|
|
|
2017-09-04 03:20:35 +00:00
|
|
|
See: [Scopes and resources](https://hexdocs.pm/phoenix/Phoenix.Router.html#module-scopes-and-resources) _(hexdocs.pm)_
|
|
|
|
|
|
|
|
### Scoped routes
|
2016-06-02 10:07:58 +00:00
|
|
|
|
|
|
|
```elixir
|
|
|
|
scope "/admin" do
|
|
|
|
pipe_through :browser
|
|
|
|
resources "/reviews", MyApp.Admin.ReviewController
|
|
|
|
end
|
|
|
|
# reviews_path() -> /admin/reviews
|
|
|
|
```
|
|
|
|
|
|
|
|
```elixir
|
|
|
|
scope "/admin", as: :admin do: ... end
|
|
|
|
# admin_reviews_path() -> /admin/reviews
|
|
|
|
```
|
2017-09-04 03:20:35 +00:00
|
|
|
|
|
|
|
See: [scope/2](https://hexdocs.pm/phoenix/Phoenix.Router.html#scope/2) _(hexdocs.pm)_
|