Skip to content
Tutorials Center
My learning

Topics

Lesson 4 of 38 ยท Getting Started

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 ships with many Mix tasks, command-line helpers that generate code, inspect your app and manage the database. Learning the handful you use every day saves hours. This lesson covers the generators, the inspection tasks, debugging in IEx and the LiveDashboard that comes with new projects.

You should have a generated project from installing Phoenix. Remember that all commands here run on your own machine; the site's TryIts only model the ideas.

Discovering tasks

Terminal
mix help | grep phx       # list Phoenix tasks
mix help phx.gen.html     # read the documentation for one task

Every task documents its options with mix help <task>, so you never need to memorize flags.

Everyday tasks

Task What it does
mix phx.server Start the endpoint
mix phx.routes Print every route with its controller and action
mix ecto.create / ecto.drop Create or delete the database
mix ecto.gen.migration add_posts Create an empty migration
mix ecto.migrate / ecto.rollback Apply or undo migrations
mix ecto.reset Drop, create, migrate and seed
mix test Run tests
mix format Format all code
mix deps.get Install dependencies
mix phx.digest Fingerprint static files for production
mix phx.gen.secret Print a random secret for SECRET_KEY_BASE

The route inspector

mix phx.routes is the quickest way to answer "what does my app respond to?".

TEXT
$ mix phx.routes
  GET     /               BlogWeb.PageController :home
  GET     /posts          BlogWeb.PostController :index
  GET     /posts/new      BlogWeb.PostController :new
  GET     /posts/:id      BlogWeb.PostController :show
  POST    /posts          BlogWeb.PostController :create

Newer Phoenix versions print the routes from the router when you run the task; the output format can differ slightly between versions. In dev you can also see the route list in the browser's error page when no route matches.

Generators

Generators write real code into your project so you can read and edit it. They come in four main flavors:

Terminal
# HTML CRUD: context, schema, migration, controller, templates, tests
mix phx.gen.html Blog Post posts title:string body:text published:boolean

# JSON CRUD for APIs
mix phx.gen.json Blog Post posts title:string body:text

# LiveView CRUD
mix phx.gen.live Blog Post posts title:string body:text

# Only the context and schema, no web layer
mix phx.gen.context Blog Post posts title:string body:text

# Only a schema and migration
mix phx.gen.schema Blog.Comment comments body:text post_id:references:posts

# Authentication system (covered in a later chapter)
mix phx.gen.auth Accounts User users

The arguments are context name, schema name, table name, then field:type pairs. After generating you must add the printed route to the router and run mix ecto.migrate.

Generators are for learning and for saving boilerplate. Read the output, rename things and delete what you do not need. Nothing stays tied to the generator afterward.

Code reloading and live reload

In development, Phoenix watches lib/, recompiles changed modules on the next request and, via the live reloader, refreshes the browser when templates, CSS or JS change. This is why you rarely restart the server. Configuration files, mix.exs and dependencies are not reloaded; restart for those.

Debugging in IEx

Run the server inside IEx to call your code live:

Terminal
iex -S mix phx.server
ELIXIR
iex> recompile()                    # compile after editing, if needed
iex> Blog.list_posts()              # call your own context
iex> BlogWeb.Router.__routes__()    # inspect routes as data
iex> h Blog.list_posts              # show docs

To pause inside running code, add IEx.pry() or require IEx; IEx.pry() in a function, run the server under IEx, trigger the request and you land in a shell with the local variables available. For quick checks, IO.inspect(value, label: "params") prints and returns the value, so you can drop it into the middle of a pipe:

ELIXIR
params
|> IO.inspect(label: "incoming")
|> Blog.create_post()

LiveDashboard

New projects mount Phoenix.LiveDashboard at /dev/dashboard in development. It shows request metrics, memory, the process list, Ecto queries stats and ETS tables, all updating live. To expose it in production, put it behind authentication:

ELIXIR
import Phoenix.LiveDashboard.Router

scope "/" do
  pipe_through [:browser, :require_admin]
  live_dashboard "/dashboard", metrics: BlogWeb.Telemetry
end

Without that pipeline anyone could read internal metrics, so never mount it openly in production.

Modeling a task runner

A Mix task is just a module with a run/1 function receiving the argument list. This stand-in imitates how mix finds and runs one.

JS A tiny task registry

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Parsing generator arguments

The generator turns title:string body:text into schema fields and a migration. Here is the parsing idea.

JS Parse field:type pairs

Press Run (or Ctrl + Enter) to see the console output here.

No output.

The real generator does the same string work and then writes the files from templates.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. Which command shows all routes of the app?

    mix phx.routes prints each route with its HTTP verb, path, controller and action.
  2. 2. What do the arguments of mix phx.gen.html Blog Post posts title:string mean?

    You give the context name, the schema name, the plural table name and then field:type pairs.
  3. 3. What is not hot-reloaded in development?

    Changing mix.exs, config files or dependencies requires a restart.
  4. 4. Why protect LiveDashboard in production?

    The dashboard reveals sensitive internals, so mount it behind an admin-only pipeline.
  5. 5. What does IO.inspect(value, label: "x") return?

    It prints and returns the value unchanged, so it can sit inside a pipeline.

Summary

  • mix help <task> documents any task; phx.routes, ecto.migrate and test are daily drivers.
  • Generators (phx.gen.html, json, live, context, schema, auth) write editable code from context, schema, table and fields.
  • Run iex -S mix phx.server to call your code live and use IO.inspect or IEx.pry to debug.
  • LiveDashboard shows live metrics; protect it in production.

Next: Phoenix and the BEAM