This page is not available in every language. Missing translations are disabled in the language selector.

Quick reference

Phoenix Framework Cheatsheet

A comprehensive reference guide for Phoenix: Elixir web framework quickstart, directory layout, routing, Plug.Conn pipelines, Ecto migrations, and controllers.

· Published

Phoenix is a productive, reliable web framework built on the Elixir and Erlang BEAM runtime.

Quickstart

# Install Hex package manager and Phoenix installer archive
mix local.hex
mix archive.install hex phx_new

# Generate new Phoenix application with PostgreSQL
mix phx.new my_app

# Start the interactive development server
mix phx.server

Application Directory Layout

my_app/
├── assets/          # Frontend assets (esbuild, Tailwind CSS)
├── config/          # Environment configuration (dev, test, prod)
├── lib/
│   ├── my_app/      # Core business domain logic and Ecto schemas
│   ├── my_app.ex
│   └── my_app_web/  # Web presentation layer (controllers, views, router)
│       ├── controllers/
│       ├── router.ex
│       └── endpoint.ex
├── priv/
│   └── repo/        # Database migrations and seed fixtures
└── test/            # ExUnit test suites

Controllers & Pipelines

Phoenix uses Plug.Conn as the unified HTTP transformation struct:

defmodule MyAppWeb.UserController do
  use MyAppWeb, :controller

  def show(conn, %{"id" => id}) do
    user = MyApp.Accounts.get_user!(id)

    conn
    |> put_status(200)
    |> render("show.html", user: user)
  end

  def api_index(conn, _params) do
    users = MyApp.Accounts.list_users()
    json(conn, %{data: users})
  end
end

Generators & Contexts

# Generate full HTML CRUD context, schema, controller, and templates
mix phx.gen.html Accounts User users name:string email:string:unique

# Generate JSON API resources
mix phx.gen.json Catalog Product products title:string price:decimal

# Run database migrations
mix ecto.migrate

References

Related topics