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
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
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.
get, post, resources, and live; route params and verified ~p paths.:browser, :api, and auth as a pipeline.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
- Phoenix — Routing — verbs, paths, resources, and scopes.
- Phoenix — Router — the router macros in reference form.
- Phoenix — Controllers — actions, params, and rendering.
- Elixir — Plug — the composable connection-in, connection-out contract.
Related in this course
- F6.02.1 · Routes & verbs
- F6.02.2 · Pipelines & scopes
- F6.02.3 · Writing a plug
- F6.01 · Architecture & the request lifecycle — the pipeline this module fills in.
- F6 · Phoenix Framework