mix phx.gen.html (Phoenix v1.6.2) View Source
Generates controller, views, and context for an HTML resource.
mix phx.gen.html Accounts User users name:string age:integer
The first argument is the context module followed by the schema module and its plural name (used as the schema table name).
The context is an Elixir module that serves as an API boundary for the given resource. A context often holds many related resources. Therefore, if the context already exists, it will be augmented with functions for the given resource.
Note: A resource may also be split over distinct contexts (such as
The schema is responsible for mapping the database fields into an
Elixir struct. It is followed by an optional list of attributes,
with their respective names and types. See
for more information on attributes.
Overall, this generator will add the following files to
- a context module in
lib/app/accounts.exfor the accounts API
- a schema in
lib/app/accounts/user.ex, with an
- a view in
- a controller in
- default CRUD templates in
A migration file for the repository and test files for the context and controller features will also be generated.
The location of the web files (controllers, views, templates, etc) in an
umbrella application will vary based on the
:context_app config located
in your applications
:generators configuration. When set, the Phoenix
generators will generate web files directly in your lib and test folders
since the application is assumed to be isolated to web specific functionality.
:context_app is not set, the generators will place web related lib
and test files in a
web/ directory since the application is assumed
to be handling both web and domain specific functionality.
config :my_app_web, :generators, context_app: :my_app
--context-app option may be supplied to the generator:
mix phx.gen.html Sales User users --context-app warehouse
By default, the controller and view will be namespaced by the schema name.
You can customize the web module namespace by passing the
--web flag with a
module name, for example:
mix phx.gen.html Sales User users --web Sales
Which would generate a
In some cases, you may wish to bootstrap HTML templates, controllers,
and controller tests, but leave internal implementation of the context
or schema to yourself. You can use the
flags for file generation control.
You can also change the table name or configure the migrations to
use binary ids for primary keys, see
mix phx.gen.schema for more