Overview
ThePhoenix.LiveView.Router module provides macros and functions for defining LiveView routes in Phoenix applications. It enables you to mount LiveViews using the live/4 macro and organize related LiveViews into live sessions.
Macros
live/4
Defines a LiveView route.The URL path for the LiveView route
The LiveView module to mount
Optional action name that will be available as
@live_action in the LiveViewOptions for configuring the route:
:container- Tuple for HTML tag and attributes for the LiveView container, e.g.,{:li, style: "color: blue;"}:as- Configure the named helper (defaults to:liveor LiveView name when using actions):metadata- Map of metadata for telemetry events and route info:private- Map of private data to put in the plug connection
The HTTP request method that a route defined by
live/4 responds to is GET.Examples
Basic LiveView route:live_session/3
Defines a live session for live redirects within a group of live routes.Unique name for the live session
Options for configuring the session:
:session- Extra session map or MFA tuple to merge with the LiveView session:root_layout- Root layout tuple for initial HTTP render:on_mount- List of hooks to attach to mount lifecycle of each LiveView:layout- Layout the LiveView will be rendered in
Block containing
live/4 route definitionsExamples
Basic live session:Security Considerations
live_session draws boundaries between groups of LiveViews. Key security points:
- Navigation between sessions: Redirecting between different
live_sessions forces a full page reload and establishes a new LiveView connection - Authentication: Perform authentication checks in
on_mountcallbacks, as navigation within a session does not go through the plug pipeline - Authorization: Always validate on mount (page access) and handle_event (action permissions)
Functions
fetch_live_flash/2
Fetches the LiveView flash and merges with controller flash.The connection struct
Options (currently unused)
Connection with merged flash
:fetch_flash plug used by Phoenix.Router.