jonnifyelixir
F6 · Phoenix Framework / F6.02 · routing

F6 · the architecture · module 2

Routing, controllers & plugs

F6.01 showed the request lifecycle as one composed pipeline; this module builds out its middle. A route maps a verb and a path to one controller action. A pipeline is a named, reusable stack of plugs that runs before the action — fetch the session, reject a forged request, require a logged-in user — and a scope runs a group of routes through it. The single idea underneath all three is the plug: a function that takes a connection and returns a connection, so routing, pipelines, and your own middleware all compose the same way. Get this layer right and cross-cutting concerns live in one place instead of being copied into every action.

This module covers the three pieces of the match: routes and verbs, named pipelines and scopes, and writing a plug of your own — all of which sit in front of the thin controller from F6.01.

One request, matched to one action

REQUEST GET /courses/42 ROUTER POST /courses :create × GET /courses :index × GET /courses/:id :show ✓ ACTION CourseController, :show params: %{"id" => "42"} top to bottom · first match wins verb and path together pick the action

GET /courses/42 → GET /courses/:id
First verb-and-path match wins — here, the :show action with id = 42.

Three pieces, one idea

Routes, pipelines, and plugs are layers of the same abstraction. A route names an action; a pipeline is a stack of plugs that runs first; a plug is the connection-in, connection-out function they are all built from. Select one to see its role.

The routing layer · select a piece

ROUTE get "/courses/:id", CourseController, :show F6.02.1 PIPELINE pipeline :browser do plug ... end — a named stack F6.02.2 PLUG call(conn, opts) :: conn — the shared contract F6.02.3

piece: Route

is: verb + path → an action

One abstraction, three uses. Because a pipeline is a stack of plugs and the router itself is a plug, “learning routing” is mostly learning the plug contract and where each plug runs.

Three deep dives

In order: how routes map verbs and paths to actions; how named pipelines and scopes group the plugs that run first; and how to write a plug of your own, the contract everything in the request path shares.

F6.02.1
Routes & verbs
get, post, resources, and live; route params and verified ~p paths.
F6.02.2
Pipelines & scopes
A pipeline is a named stack of plugs; a scope runs routes through it. :browser, :api, and auth as a pipeline.
F6.02.3
Writing a plug
init/1 and call/2, conn in and conn out, and halt/1 to stop the pipeline early.

F6.01 · the lifecycle

A request reaches a thin controller that calls the facade. The router is the plug that put it there.

F6.02 · the match

Routes, pipelines, and plugs shape what runs before the action — in one place, not copied per action.

Start with routes and verbs, then pipelines and scopes, then writing a plug. This module follows F6.01 — the request lifecycle — and the next, F6.03, puts Ecto behind the facade.

References

Routing, controllers, and the plug contract the whole pipeline is built from.

Sources

Related in this course