Kurisu

Kurisu

Is there any difference between these two groups of Phoenix live routes definitions?

To me, there seems to be no difference, but I would like to confirm if I’m correct. If I am, please let me know if there are any considerations I should be aware of in your opinion. Could you please take a look? Thank you.

The first group contains a Plugs pipeline inside the live_session for the admin, while the second group has the Plugs pipeline outside the live_session but scoped to it. Here are the two code examples for reference:

First group:

live_session :admin, on_mount: MyAppWeb.AdminLiveAuth do
  scope "/" do
    # Plugs pipeline inside live_session
    pipe_through [MyAppWeb.AdminPlugAuth]

    # Live routes
    live "/admin", AdminDashboardLive, :index
    live "/admin/posts", AdminPostLive, :index
  end
end

Second group:

scope "/" do
  # Plugs pipeline outside live_session but scoped to it
  pipe_through [MyAppWeb.AdminPlugAuth]
  
  live_session :admin, on_mount: MyAppWeb.AdminLiveAuth do  
    # Live routes
    live "/admin", AdminDashboardLive, :index
    live "/admin/posts", AdminPostLive, :index
  end
end

Marked As Solved

maria456

maria456

You’re right, both code snippets achieve the same outcome: the MyAppWeb.AdminPlugAuth pipeline will be applied to the /admin and /admin/posts live routes. The main difference is code organization. The first group keeps the plug pipeline inside the live_session, which might be more readable for associating authentication with the session. The second group places it outside but scoped to the session, offering slightly more flexibility if you later need routes within the same scope without admin authentication. The order of execution is slightly different but unlikely to have practical consequences in most scenarios. Choose the style you find more maintainable.

Also Liked

BartOtten

BartOtten

There is no difference.

Scopes simply augment any route (or nested scope) in their body. So does live_session. In the end all that’s left is a list of Route structs.

Technically the order does not matter.

If you want to confirm you can use Routex and write a simple extension module with…

defmodule InspectExtension do
  transform(routes, _backend, _env) do
    IO.inspect(routes)
  end
end

Both variants should render the exact same list of routes.

(from mobile, untested, if you do feel free to open a PR as it’s a nice addition)

Where Next?

Popular in Questions Top

mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New
vac
Hi, I’m quite new in Elixir and I’m trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and I...
New
RisingFromAshes
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New

Other popular topics Top

JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New
gausby
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
1207 39297 209
New
RisingFromAshes
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
AstonJ
We’ve put together this wiki for Phoenix LiveView - please feel free to add any info you feel is worth including. What is Phoenix LiveV...
New
marick
I had some trouble figuring out how to make many-to-many associations work. Once I got it working, I wrote a blog post. Because I’m a nov...
New

We're in Beta

About us Mission Statement