100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
Programming

Phoenix Framework Basics

An introduction to Phoenix's router, controllers, contexts, and plug pipeline — the core building blocks of a Phoenix web application.

Practical ElixirBeginner9 min readJul 10, 2026
Analogies

What Is the Phoenix Framework?

Phoenix is a web framework for Elixir, built on top of the Plug specification and the Cowboy (or, since Phoenix 1.7, Bandit) HTTP server. It follows a structure reminiscent of Ruby on Rails' MVC pattern but leans heavily on Elixir's concurrency model: every HTTP request is handled by its own lightweight BEAM process, so a slow request never blocks others. This makes Phoenix well suited for high-throughput APIs and real-time features like WebSockets and Server-Sent Events.

🏏

Cricket analogy: Comparing Phoenix's speed to how MS Dhoni finishes matches with the same calm efficiency under pressure — Phoenix keeps handling thousands of connections without breaking a sweat, thanks to the BEAM.

Router, Controllers, and Views

The router.ex file defines every route the application responds to, mapping an HTTP verb and path to a specific controller action, for example get "/posts", PostController, :index. Controllers receive the connection (%Plug.Conn{}), fetch or manipulate data, and render a response — typically a HEEx template or a JSON payload via render/3 or json/2. This separation keeps routing concerns distinct from the logic that produces a response.

🏏

Cricket analogy: The router.ex file is like the team sheet at Eden Gardens naming which bowler (controller action) is bowling each over (route), so everyone knows exactly what happens for a given ball.

elixir
defmodule MyAppWeb.Router do
  use MyAppWeb, :router

  pipeline :browser do
    plug :accepts, ["html"]
    plug :fetch_session
    plug :fetch_flash
    plug :protect_from_forgery
  end

  scope "/", MyAppWeb do
    pipe_through :browser

    get "/", PageController, :home
    resources "/posts", PostController
  end
end

Contexts: Organizing Business Logic

Contexts are plain Elixir modules that group related schemas and functions around a business domain, such as Accounts or Blog. Rather than controllers calling Ecto.Repo directly, they call context functions like Accounts.get_user!(id) or Blog.create_post(attrs), which keeps persistence details and validation logic out of the web layer. mix phx.gen.context Blog Post posts title:string body:text scaffolds a context along with its schema and migration.

🏏

Cricket analogy: A context like Accounts is like a franchise's dedicated batting coach who owns all decisions about batting technique, separate from the bowling coach (a different context), keeping responsibilities cleanly divided.

Plugs and the Request Pipeline

A Plug is any module implementing init/1 and call/2, or a function of arity 2, that transforms a %Plug.Conn{}. The router groups plugs into pipelines — :browser typically includes fetch_session, fetch_flash, and protect_from_forgery, while :api might just include accepts: ["json"]. Every request passing through a scope runs through its pipeline's plugs in order before reaching the controller action.

🏏

Cricket analogy: The plug pipeline is like the standard warm-up drills every IPL team runs before a match — stretching, catching practice, fitness checks — each player passes through the same sequence before taking the field.

Phoenix generators like mix phx.gen.html, mix phx.gen.json, and mix phx.gen.live scaffold a working context, schema, migration, controller, and templates in seconds — but always read and adjust the generated code; generators optimize for a common case, not your specific authorization or validation rules.

  • Phoenix runs on Plug and Cowboy/Bandit, handling each request in its own lightweight BEAM process.
  • router.ex maps HTTP verb + path combinations to controller actions via pipelines and scopes.
  • Controllers receive a %Plug.Conn{}, call context functions, and render a template or JSON response.
  • Contexts (e.g., Accounts, Blog) encapsulate business logic and keep persistence out of the web layer.
  • Plugs are composable request/response transformers chained together in pipelines like :browser and :api.
  • mix phx.gen.* generators scaffold contexts, schemas, controllers, and templates but should be reviewed, not trusted blindly.

Practice what you learned

Was this page helpful?

Topics covered

#Programming#ElixirStudyNotes#PhoenixFrameworkBasics#Phoenix#Framework#Router#Controllers#StudyNotes#SkillVeris#ExamPrep