Where is `live_path` defined?

Hello! I’m new to elixir/phoenix/the forum, and I’m having a hard time finding the definition of MyApp.Router.Helpers.live_path (which usually shows up as Routes.live_path since Phoenix aliases it).

I’ve used the search in hexdocs for both Phoenix and LiveView, and also in both GitHub repos. I’m guessing it’s generated by a macro or some Elixir magic, but I can’t figure it out. Any help is much appreciated.

1 Like

phoenix_live_view/helpers.ex at master · phoenixframework/phoenix_live_view · GitHub?

Is it there? Again, I might be missing something (noob here, so if it’s in a macro or something magical I might not see it), but I’ve already looked there, and searching for “path” on that page gives me 8 results, all of which are in the docs sections.

Sorry, wrong link. Replying from mobile which makes it not so easy to track the code. Guess LiveView uses the Phoenix router helpers generator.

1 Like

As I suspected, it seems quite magical to me. I still don’t really understand it, but I’m guessing that the :"#{helper}_path"s in that function somehow become live_path definitions when you use Phoenix.Router in the MyAppWeb module in your app, and maybe there’s a generated LiveController (?)

Thanks! I’ll mark that as the solution.

Since that’s a part of the API, and I’d guess it’s a pretty frequent use one for LiveView apps, should that be documented somehow? (Assuming it isn’t already there, and I just missed it) This is what seems to be the documentation of that behavior on the Phoenix.Router side: Phoenix.Router — Phoenix v1.5.8

This all started because I was trying to figure out why it worked correctly when I accidentally passed a map instead of a keyword list as the last parameter to live_path, and I couldn’t find any description of the function. The iex help just says def live_path(conn_or_endpoint, action, params).

I guess this is why it takes a map: Live routes assign to a session instead of the conn.assigns you are familiar with from ‘dead’ routes.

a map to be merged into the session, for example: %{"my_key" => 123} . The map keys must be strings


Ps. Still at mobile so can’t examine the source.

It all makes sense now. I assumed the live_path function was inherently different from the usual route helpers, since the name was the same for all my different LiveView modules, instead of being specific, the way normal route helpers are, e.g. users_path.

Then, in the last link you shared, I read that the as option defaults to :live, and that’s why it’s always live_path.

I should have understood that from your first answer. Thanks again!

1 Like