Showing routes

mix phx.routes        # 1.3+
mix phoenix.routes    # 1.2 and below

See: Mix.Tasks.Phoenix.Routes (hexdocs.pm)

Single routes

get "/", PageController, :index

Also: put post patch options delete head

Resources

resources "/users", UserController
resources "/users", UserController, only: [:index, :show]
resources "/users", UserController, except: [:delete]
resources "/users", UserController,
  as: :person    # helper name (person_path)
  name: :person  # ...?
  param: :id     # name of parameter for this resource

Generates these routes:

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)

See: resources/4 (hexdocs.pm)

Path helpers

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
user_url(conn, :index) # → "http://localhost:4000/users"
MyApp.Router.Helpers.user_path(MyApp.Endpoint, :index)

See: Helpers (hexdocs.pm)

Nested resources

resources "/users", UserController do
  resources "/posts", PostController
end
user_post_path(:index, 17)     # → /users/17/posts
user_post_path(:show, 17, 12)  # → /users/17/posts/12

See: Scopes and resources (hexdocs.pm)

Scoped routes

scope "/admin" do
  pipe_through :browser
  resources "/reviews", MyApp.Admin.ReviewController
end
# reviews_path() -> /admin/reviews
scope "/admin", as: :admin do: ... end
# admin_reviews_path() -> /admin/reviews

See: scope/2 (hexdocs.pm)

0 Comments for this cheatsheet. Write yours!