2 title: "Phoenix: Routing"
12 mix phoenix.routes # 1.2 and below
15 See: [Mix.Tasks.Phoenix.Routes](https://hexdocs.pm/phoenix/Mix.Tasks.Phoenix.Routes.html) _(hexdocs.pm)_
20 get "/", PageController, :index
23 Also: `put` `post` `patch` `options` `delete` `head`
28 resources "/users", UserController
29 resources "/users", UserController, only: [:index, :show]
30 resources "/users", UserController, except: [:delete]
34 resources "/users", UserController,
35 as: :person # helper name (person_path)
37 param: :id # name of parameter for this resource
40 Generates these routes:
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)` |
53 See: [resources/4](https://hexdocs.pm/phoenix/Phoenix.Router.html#resources/4) _(hexdocs.pm)_
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
65 user_url(conn, :index) # → "http://localhost:4000/users"
69 MyApp.Router.Helpers.user_path(MyApp.Endpoint, :index)
72 See: [Helpers](https://hexdocs.pm/phoenix/Phoenix.Router.html#module-helpers) _(hexdocs.pm)_
77 resources "/users", UserController do
78 resources "/posts", PostController
83 user_post_path(:index, 17) # → /users/17/posts
84 user_post_path(:show, 17, 12) # → /users/17/posts/12
87 See: [Scopes and resources](https://hexdocs.pm/phoenix/Phoenix.Router.html#module-scopes-and-resources) _(hexdocs.pm)_
94 resources "/reviews", MyApp.Admin.ReviewController
96 # reviews_path() -> /admin/reviews
100 scope "/admin", as: :admin do: ... end
101 # admin_reviews_path() -> /admin/reviews
104 See: [scope/2](https://hexdocs.pm/phoenix/Phoenix.Router.html#scope/2) _(hexdocs.pm)_