markomitranic
Summary: When using Phoenix Routes helper in tests, its unable to resolve an action which uses a different controller.
Hey gang, first time writing here ![]()
If i have separate scopes like /users/ and /internal/ I may want to re-use the same controller action between the two of them. For example :show. However when I do that, the Router Helper is unable to see two distinct items - since they both belong to the same controller. Here is an example:
In our router, we have two distinct scopes, each with their own auth and middleware pipelines.
scope "/public", MarkoWeb do
pipe_through :api
resources "/users", UserController
end
scope "/private", MarkoWeb, as: :private do
pipe_through :internal_api
get "/media/:id", InternalController, :fetch_media
get "/users/:id", UserController, :show
get "/posts/:id", PostController, :show
get "/items/:id", ItemController, :show, as: :show_item
end
All is well, and the APIs work just great. However when I attempt to use the helper to get their respective paths, all hell breaks loose. Here are some examples and 3 questions I have:
# Works fine
Routes.user_path(conn, :fetch_media, id)
Routes.private_internal_path(conn, :fetch_media, id)
# 1. The helper does not work when controller is reused.
# 2. Users and posts share the same function name.
Routes.private_internal_path(conn, :show, id)
# 3. Alias does not work at all
Routes.private_internal_path(conn, :show_item, id)
I guess, I could make the controllers just empty facades, and then have separate controllers for each one, which use the same domain logic below, which is what I’m doing right now.
Hope that sums it up
I’m just trying to understand how this “meta-magic” exactly works.
Thanks for any responses !
Trending in Questions
Other Trending Topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #blog-post
- #elixir-ls
- #ai
- #elixirconf-us
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming










Showing Posts 1 to 5- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
markomitranic
Here is another alternative I’ve picked up along the way - don’t reuse controller, but reuse the view:
However, this does not work when your controller has actuall business logic in it, since you’d have to copy-pasta it.
LostKobrakai
:aschanges the function name on the route helper, not the action.markomitranic
Hey LostKobraKai, thank you for your response!
Yeah that is percicely what I’ve discovered as well by reading the docs at Phoenix.Router — Phoenix v1.8.8 .
Then I’ve tried
Routes.show_item_path(conn, :show, Faker.UUID.v4())but that does not seem how it maps to the alias. Perhaps because the route belongs in anas: :privatescope?LostKobrakai
Yeah,
:ason scopes is used as a prefix on the name.markomitranic
Bingo
Thank you