Nested LiveView Routes

Is there a LiveView equivalent to:

resources "/foo", FooController do
  get "/bar", BarController, :index

I found handle_params(params, url, socket) that could be used to capture the params in the LiveView. Or I could potentially call live_render from a standard controller; but really I’m hoping to be able to rely on @derive {Phoenix.Param, key: :slug} in my schemas to return on a hierarchical url like /<sport>/<team>/<result>.

In case I’m completely missing the picture, or there is a better way to go about this:

  • The URL structure is mirroring a simple schema structure.
defmodule Sport do
  schema "sports" do
    field :name, :string
    field :slug, :string

    has_many :teams, Team

defmodule Team do
  schema "teams" do
    field :name, :string
    field :slug, :string
    belongs_to :sport, Sport
    has_many :results, Result

defmodule Result do
  schema "results" do
    field :name, :string
    field :alias, :string
    belongs_to :team, Team
  • And the unique constraints are equally simple.
create unique_index(:sports, [:slug])
create unique_index(:teams, [:slug, :sport_id])
create unique_index(:results, [:slug, :team_id])

Is there a clean, effecient way of:

  • a) returning records based on params like /bobsleigh/cool_runnings/olympics_94
  • b) nesting many live routes to keep Router concise?


1 Like

I think you can just do:

live "/:sport_slug/:team_slug/:result_slug", ResultLive, :index

and then the params should be

%{"sport_slug" => "bobsleigh", "team_slug" => "cool_runnings", "result_slug" => "olympics_94"}