Phoenix Framework Tutorial
Learn Phoenix Framework step by step: 38 lessons in 9 chapters with examples, quizzes, diagrams and projects.
Start learning →
of lessons completed
Getting Started
- What Is Phoenix? Learn what the Phoenix web framework is, why it runs on Elixir and the BEAM, and how a request travels through endpoint, router, controller and view.
- Installing Phoenix and Creating a Project Install Elixir, Erlang, PostgreSQL and the Phoenix generator, then create and run your first Phoenix app with mix phx.new and see the welcome page.
- Project Structure and Configuration Tour a generated Phoenix project: lib, lib_web, priv, config, assets and tests, and how config.exs, dev.exs, prod.exs and runtime.exs fit together.
- Mix Tasks and Developer Tools Learn the Phoenix mix tasks you use daily: phx.server, phx.routes, phx.gen generators, ecto tasks, IEx debugging, code reloading and LiveDashboard.
- Phoenix and the BEAM: Processes and Supervision See how Phoenix applications use BEAM processes, the application supervision tree, the endpoint, the Ecto pool and PubSub to stay fast and fault tolerant.
Routing, Controllers and Plugs
- Router, Scopes and Pipelines Learn how the Phoenix router maps requests to controllers, groups routes with scope, applies plugs with pipelines and how route matching order works.
- Controllers, Params and Responses Write Phoenix controller actions: read path, query and body params, pattern match them, render pages, redirect, set status codes, put flash messages and use sessions.
- Plugs in Depth: Function and Module Plugs Understand Plug, the specification behind Phoenix: write function plugs and module plugs with init/1 and call/2, assign data, halt requests and test plugs.
- Resources, Verified Routes and Nested Routes Use the resources macro for RESTful routes, generate safe links with ~p verified routes, nest resources, restrict actions and build URLs in controllers and tests.
- Building JSON APIs and Fallback Controllers Build a JSON API in Phoenix with the api pipeline, JSON modules, status codes, action_fallback and consistent error responses, plus CORS and versioning notes.
Templates and Components
- HEEx Templates: Syntax and Escaping Learn HEEx, Phoenix's HTML-aware template language: interpolation, if and for, assigns, attributes, comments and automatic escaping that blocks XSS.
- Function Components, Attributes and Slots Build reusable HEEx function components with attr, slot and render_slot: declare types and defaults, pass inner content, use named slots and global attributes.
- Layouts, Core Components and Navigation Understand root and app layouts, the generated CoreComponents module (flash, button, input, table, modal), link helpers and how to add your own shared layout.
- Forms, Changesets and to_form Build Phoenix forms with to_form, the form component and input fields: show validation errors, protect with CSRF, handle uploads and nested inputs.
Ecto Schemas and Queries
- Ecto Schemas, Migrations and the Repo Define Ecto schemas, create and run migrations, add indexes and constraints, and use the Repo for insert, get, update and delete in a Phoenix application.
- Ecto Queries: from, where, join and aggregates Write composable Ecto queries with the from macro: where filters, order_by, limit, select, joins, group_by aggregates, dynamic filters and avoiding SQL injection.
- Changesets: Casting, Validation and Constraints Master Ecto changesets: cast and whitelist params, run built-in and custom validations, handle unique constraint errors, and use separate changesets per use case.
- Associations, Preloading and Contexts Model has_many, belongs_to and many_to_many relations in Ecto, avoid N+1 queries with preload, and organize business logic into Phoenix contexts.
LiveView
- LiveView Lifecycle: Mount, Render and Assigns Learn how a Phoenix LiveView starts: the two mounts, connected?/1, assigns, handle_params/3, the ~H template and how change tracking sends tiny diffs.
- LiveView Events and Forms Handle browser events in Phoenix LiveView: phx-click, phx-value, phx-change and phx-submit, to_form/2, changesets, live validation, debounce and errors.
- LiveView Streams, Navigation and JS Commands Render big lists with LiveView streams, move between pages with patch and navigate, and run client-side effects with Phoenix.LiveView.JS commands.
- Live Components, Async Work and Hooks Split LiveView into function components and stateful LiveComponents, load slow data with assign_async and start_async, and bridge to JavaScript with hooks.
- LiveView Uploads Handle file uploads in LiveView with allow_upload, live_file_input, progress, previews, validation errors and consume_uploaded_entries, including external storage.
Channels and PubSub
- Phoenix Channels and Sockets Build realtime features with Phoenix Channels: UserSocket, topic patterns, join/3, handle_in/3, push and broadcast, the JavaScript client and the wire format.
- Phoenix PubSub and Broadcasting Use Phoenix.PubSub to push updates between processes: subscribe, broadcast, handle_info, broadcasting from contexts into LiveView, topic design and clusters.
- Phoenix Presence: Who Is Online Learn Phoenix.Presence: track and list users, understand metas and presence_diff, show an online list in LiveView and why it works across a cluster.
Authentication and Security
- Authentication with mix phx.gen.auth Understand what mix phx.gen.auth generates: users, hashed passwords, session tokens, magic links, the UserAuth plug, on_mount hooks and logout handling.
- Authorization and Scopes in Phoenix Decide what each signed-in user may do: Phoenix 1.8 scopes, scoped context functions, ownership checks, role policies, and how to prevent IDOR bugs.
- Web Security in Phoenix Secure a Phoenix app: CSRF tokens, automatic XSS escaping and raw/1 risks, SQL injection and Ecto fragments, security headers, CSP, check_origin, HTTPS and rate limiting.
- API Tokens and Session Authentication Authenticate API clients in Phoenix: signed tokens with Phoenix.Token, database-backed API tokens, a bearer-token plug, expiry, revocation and rate limiting.
Testing and Deployment
- Testing Controllers and Contexts Test a Phoenix app with ExUnit: DataCase for contexts, ConnCase for controllers, fixtures, the Ecto SQL sandbox, authenticated tests, JSON APIs and mocking with Mox.
- Testing LiveView Test LiveViews without a browser: live/2, element, render_click, form render_change and render_submit, has_element?, patch and redirect asserts, PubSub and async.
- Releases, Configuration and Deployment Deploy a Phoenix app: mix release, config/runtime.exs and environment variables, migrations in releases, Docker, health checks, clustering and zero-downtime tips.
- Telemetry, Logging and Observability See what your Phoenix app is doing: telemetry events and metrics, LiveDashboard, Logger metadata, custom events with :telemetry.execute and error tracking.
Projects and Practice
- Project: A Live Poll with LiveView and PubSub Build a real-time poll in Phoenix: Ecto schemas, a context with validation, a LiveView page, PubSub broadcasts so every viewer sees votes appear instantly.
- Project: A Chat Room with Channels and Presence Build a chat room in Phoenix: an authenticated socket, a room channel with join checks, message validation, history on join, typing and presence, plus a JavaScript client.
- Project: A JSON Notes API Build a token-protected JSON API in Phoenix: routes, a context with ownership checks, JSON views, FallbackController errors, pagination, filtering and tests.
- Practice, Review and Next Steps Consolidate your Phoenix skills: a request-lifecycle review, common mistakes, a decision guide for LiveView vs controllers vs channels, practice tasks and a learning path.