View Source mix phx.gen.live (Phoenix v1.6.16)
Generates LiveView, templates, and context for a resource.
mix phx.gen.live Accounts User users name:string age:integer
The first argument is the context module. 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.
The second argument is the schema module. The schema is responsible for mapping the database fields into an Elixir struct.
The remaining arguments are the schema module plural name (used as the schema
table name), and an optional list of attributes as their respective names and
types. See mix help phx.gen.schema
for more information on attributes.
When this command is run for the first time, a ModalComponent
and
LiveHelpers
module will be created, along with the resource level
LiveViews and components, including UserLive.Index
, UserLive.Show
,
and UserLive.FormComponent
modules for the new resource.
Note: A resource may also be split over distinct contexts (such as
Accounts.User
andPayments.User
).
Overall, this generator will add the following files:
- a context module in
lib/app/accounts.ex
for the accounts API - a schema in
lib/app/accounts/user.ex
, with ausers
table - a LiveView in
lib/app_web/live/user_live/show.ex
- a LiveView in
lib/app_web/live/user_live/index.ex
- a LiveComponent in
lib/app_web/live/user_live/form_component.ex
- a helpers module in
lib/app_web/live/live_helpers.ex
with a modal
After file generation is complete, there will be output regarding required updates to the lib/app_web/router.ex file.
Add the live routes to your browser scope in lib/app_web/router.ex:
live "/users", UserLive.Index, :index
live "/users/new", UserLive.Index, :new
live "/users/:id/edit", UserLive.Index, :edit
live "/users/:id", UserLive.Show, :show
live "/users/:id/show/edit", UserLive.Show, :edit
the-context-app
The context app
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 (LiveView's, 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.
If :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.
Example configuration:
config :my_app_web, :generators, context_app: :my_app
Alternatively, the --context-app
option may be supplied to the generator:
mix phx.gen.live Accounts User users --context-app warehouse
web-namespace
Web namespace
By default, the LiveView modules will be namespaced by the web module.
You can customize the web module namespace by passing the --web
flag with a
module name, for example:
mix phx.gen.live Accounts User users --web Sales
Which would generate the LiveViews in lib/app_web/live/sales/user_live/
,
namespaced AppWeb.Sales.UserLive
instead of AppWeb.UserLive
.
customizing-the-context-schema-tables-and-migrations
Customizing the context, schema, tables and migrations
In some cases, you may wish to bootstrap HTML templates, LiveViews,
and tests, but leave internal implementation of the context or schema
to yourself. You can use the --no-context
and --no-schema
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 help phx.gen.schema
for more
information.