Phoenix Router는 HTTP 요청을 파이프라인을 거쳐 컨트롤러 액션 및 LiveView에 연결하는 유연하고 직관적인 라우팅 DSL을 제공합니다.
정의된 라우트 목록 확인 (Inspecting Routes)
# 전체 라우트 및 경로 헬퍼 함수 출력
mix phx.routes
리소스 기반 라우팅 (Resource Routing)
# 7개 표준 RESTful 액션 일괄 생성
resources "/users", UserController
# 생성 액션 범위 제한
resources "/posts", PostController, only: [:index, :show]
resources "/comments", CommentController, except: [:delete]
생성되는 RESTful 라우트 매핑 표
| HTTP Verb | Path | Action | Helper Function |
|---|---|---|---|
GET |
/users |
:index |
Routes.user_path(conn, :index) |
GET |
/users/new |
:new |
Routes.user_path(conn, :new) |
POST |
/users |
:create |
Routes.user_path(conn, :create) |
GET |
/users/:id |
:show |
Routes.user_path(conn, :show, id) |
GET |
/users/:id/edit |
:edit |
Routes.user_path(conn, :edit, id) |
PUT / PATCH |
/users/:id |
:update |
Routes.user_path(conn, :update, id) |
DELETE |
/users/:id |
:delete |
Routes.user_path(conn, :delete, id) |
중첩 리소스 (Nested Resources)
resources "/users", UserController do
resources "/posts", PostController
end
중첩 헬퍼 함수 자동 생성:
# Routes.user_post_path(conn, :index, user_id)
# Routes.user_post_path(conn, :show, user_id, post_id)
스코프 및 파이프라인 (Scopes & Pipelines)
pipeline :browser do
plug :accepts, ["html"]
plug :fetch_session
plug :protect_from_forgery
end
pipeline :api do
plug :accepts, ["json"]
end
scope "/api", MyAppWeb.Api, as: :api do
pipe_through :api
resources "/articles", ArticleController
end