how can I link to a live route from a regular eex template

I have a mostly static site with regular Phoenix controllers and paths. But, I now have a page where I want to use LiveView. So, I have a new route setup, but I can’t seem to be able to link to it from my regular .eex template.

Here is what it looks like:

route.ex:

  scope "/", OystersWeb do
    pipe_through [:browser, :require_authenticated_user]

    get "/users/settings", UserSettingsController, :edit
    put "/users/settings", UserSettingsController, :update
    delete "/users/settings", UserSettingsController, :delete
    post "/users/settings", UserSettingsController, :update
    put "/users/settings/update_password", UserSettingsController, :update_password
    get "/users/settings/confirm_email/:token", UserSettingsController, :confirm_email
    get "/users/threads", ApplicationController, :threads
    get "/users/saved", ApplicationController, :saved

    live "/stories/new", StoryLive, :submit_story
end

header_links.html.eex:

<span class="headerlinks">
  <a id="l_holder" style="background-color: #500000;" href="/" title="Oysters"></a>
  <%= for %{ path: path, label: label, class: class } <- @header_links do %>
    <%= link label, to: Routes.application_path(@conn, path), class: class %>
  <% end %>
</span>

I don’t have access to any live navigation in the header link template. It’s apart of the root layout that is static. Is there a way to have a link route to a LiveView page? Or do I have to turn the whole app root layout into a LiveView? I have seen some people ask about route from LiveView to regular controller routes, but I want the opposite, route from a regular controller route to a LiveView.

1 Like

Hello, @byoungdale :wave: and welcome to the community!

Looking at the examples in docs Phoenix.LiveView.Router — Phoenix LiveView v0.15.7

Looks like it should be available by something Routes.live_path(OystersWeb.Endpoint, OystersWeb.StoryLive, :submit_story)

2 Likes

There is also a usual helper that uses liveview module name like with controller, so you should be able to use also Routes.story_path(@conn, :submit_story).

2 Likes

Hello,
I’ve been adding the live routes in router.ex as i.e.
live "/samples_triage", SamplesTriageLive, :show

then in regular non-liveview template I link to it as any other links:
<%= link "Samples Triage", to: Routes.samples_triage_path(@conn, :show) %>

hth.

2 Likes

Thank you. I was using the wrong module path. My LiveView path is not apart of the Application controller so it was not in Routes.application_path(@conn, path). I needed to add:

<%= link "Submit Story", to: Routes.story_path(@conn, :submit_story) %>

Like you suggested. Thank you again!

For my use case, I had to look up the routes first. To get this to work I used index_story_path because I used a live generator.

$ mix phx.gen.live Accounts User users name:string age:integer