Learn how to use Phoenix's mix generators to rapidly scaffold CRUD functionality for your applications
Learn how to use Phoenix’s mix generators to rapidly scaffold CRUD functionality for your applications.
Mix tasks in Phoenix are command-line generators/scaffolders, similar to Rails scaffolding.
mix phx.gen.context
creates the schema, migration, and context for backend code only (no HTML/views/controllers)mix phx.gen.html
generates backend (schema, context, migration) and frontend CRUD (controllers, views, templates)mix phx.gen.json
creates backend and JSON API controllers/viewsmix phx.gen.live
makes LiveView scaffoldsTask | Schema | Migration | Context | Controller | View | LiveView |
---|---|---|---|---|---|---|
phx.gen.embedded | x | |||||
phx.gen.schema | x | x | ||||
phx.gen.context | x | x | x | |||
phx.gen.live | x | x | x | x | ||
phx.gen.json | x | x | x | x | x | |
phx.gen.html | x | x | x | x | x |
name
(string) and price
(integer):
mix phx.gen.context Consoles Console consoles name:string price:integer
mix phx.gen.html Promotions Promotion promotions name code:unique expires_at:utc_datetime
This also updates the router and supplies you with templates, a controller, forms, and even tests.mix ecto.migrate
to apply database changesrouter.ex
Frontend-focused devs may prefer just backend generators for control, but for rapid prototyping or learning Phoenix conventions, full HTML generators are very useful.
For complete documentation on Phoenix generators, see Mix.Tasks.Phx.Gen documentation.
Phoenix’s mix generators (phx.gen.context
, phx.gen.html
, etc.) help you rapidly scaffold idiomatic, fully-tested CRUD backends (and frontends) for new resources, streamlining both learning and production development.