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 !