OSDN Git Service

Regular updates
[twpd/master.git] / phoenix-routing.md
1 ---
2 title: "Phoenix: Routing"
3 category: Elixir
4 layout: 2017/sheet
5 weight: -1
6 ---
7
8 ### Showing routes
9
10 ```sh
11 mix phx.routes        # 1.3+
12 mix phoenix.routes    # 1.2 and below
13 ```
14
15 See: [Mix.Tasks.Phoenix.Routes](https://hexdocs.pm/phoenix/Mix.Tasks.Phoenix.Routes.html) _(hexdocs.pm)_
16
17 ### Single routes
18
19 ```elixir
20 get "/", PageController, :index
21 ```
22
23 Also: `put` `post` `patch` `options` `delete` `head`
24
25 ### Resources
26
27 ```elixir
28 resources "/users", UserController
29 resources "/users", UserController, only: [:index, :show]
30 resources "/users", UserController, except: [:delete]
31 ```
32
33 ```elixir
34 resources "/users", UserController,
35   as: :person    # helper name (person_path)
36   name: :person  # ...?
37   param: :id     # name of parameter for this resource
38 ```
39
40 Generates these routes:
41
42 | Method    | Path              | Helper                     |
43 | ----      | ----              | ----                       |
44 | GET       | `/users`          | `user_path(:index)`        |
45 | GET       | `/users/new`      | `user_path(:new)`          |
46 | GET       | `/users/:id`      | `user_path(:show, user)`   |
47 | GET       | `/users/:id/edit` | `user_path(:edit, user)`   |
48 | POST      | `/users`          | `user_path(:create, user)` |
49 | PATCH/PUT | `/users/:id`      | `user_path(:update, user)` |
50 | DELETE    | `/users/:id`      | `user_path(:delete, user)` |
51 {: .-left-align}
52
53 See: [resources/4](https://hexdocs.pm/phoenix/Phoenix.Router.html#resources/4) _(hexdocs.pm)_
54
55 ### Path helpers
56
57 ```elixir
58 user_path(conn, :index)                 # → /users
59 user_path(conn, :show, 17)              # → /users/17
60 user_path(conn, :show, %User{id: 17})   # → /users/17
61 user_path(conn, :show, 17, admin: true) # → /users/17?admin=true
62 ```
63
64 ```elixir
65 user_url(conn, :index) # → "http://localhost:4000/users"
66 ```
67
68 ```elixir
69 MyApp.Router.Helpers.user_path(MyApp.Endpoint, :index)
70 ```
71
72 See: [Helpers](https://hexdocs.pm/phoenix/Phoenix.Router.html#module-helpers) _(hexdocs.pm)_
73
74 ### Nested resources
75
76 ```elixir
77 resources "/users", UserController do
78   resources "/posts", PostController
79 end
80 ```
81
82 ```elixir
83 user_post_path(:index, 17)     # → /users/17/posts
84 user_post_path(:show, 17, 12)  # → /users/17/posts/12
85 ```
86
87 See: [Scopes and resources](https://hexdocs.pm/phoenix/Phoenix.Router.html#module-scopes-and-resources) _(hexdocs.pm)_
88
89 ### Scoped routes
90
91 ```elixir
92 scope "/admin" do
93   pipe_through :browser
94   resources "/reviews", MyApp.Admin.ReviewController
95 end
96 # reviews_path() -> /admin/reviews
97 ```
98
99 ```elixir
100 scope "/admin", as: :admin do: ... end
101 # admin_reviews_path() -> /admin/reviews
102 ```
103
104 See: [scope/2](https://hexdocs.pm/phoenix/Phoenix.Router.html#scope/2) _(hexdocs.pm)_