I’ve seen serval similar posts about this around here but I can’t find my exact problem.
I have a --live
generate app which nests an Admin
module in the web app with standard, non-live controllers. I use the html generator like so:
$ mix phx.gen.html --binary_id --web admin Admin.Pages Page pages example_field:string
This generates exactly what I want from a module and file structure standpoint. It creates a MyApp.Admin.Pages.Page
domain module and a MyAppWeb.Admin.PageController
web module written to lib/my_app_web/admin/page_controller.ex
. So far so good! The generated templates use Routes.admin_page_path
helpers which is also what I expect.
However…
When I update my router to look like this:
scope "/", MyAppWeb do
pipe_through :browser
live "/", HomeLive, :index
scope "/admin", Admin do
resources "/pages", PageController
end
end
or even like this:
scope "/", MyAppWeb do
pipe_through :browser
live "/", HomeLive, :index
end
scope "/admin", MyAppWeb.Admin do
pipe_through :browser
resources "/pages", PageController
end
…Phoenix generates route helpers for page
without the admin_
prefix!
I’m using Phoenix 1.5.3.
Welp, this is embarrassing. After struggling with this for an hour and deciding to post this question, I re-read the docs and turns out I originally skimmed over the part that talks, in detail, about just this case.
The answer is to use the :as
option in the scope
:
scope "/admin", MyAppWeb.Admin, as: :admin do
resources "/pages", PageController
end
Thank you, Elixir Forums, for allowing me to rubber duck you <3
1 Like
An old thread, and perhaps not necessary to post this but…
…for anyone who might stumble across this and wonder what as: :admin
is it’s from earlier versions of phoenix that leaned more heavily on named_routes, which have been replaced with verified (p sigil, tilde, ~p) routes.
Just so you’re not searching for an hour or so in the docs for examples of :as
being used in scopes.
I don’t want to revive the topic, but at the same time the addendum may save someone else’s time, since :as
is listed as an option for scope in the most recent phoenix version, and still works, according to the docs.
2 Likes
To clarify, this is a question about route helpers which were superseded by verified routes in Phoenix 1.7. Route helpers are deprecated and the :as
option should therefore be considered deprecated as well, unless it has some other use I am not aware of.
Unless you are maintaining an older app which still has route helpers this thread is effectively obsolete.
1 Like