Router Helpers with same controller at separate scopes

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 :slight_smile:
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 :slight_smile: I’m just trying to understand how this “meta-magic” exactly works.

Thanks for any responses !

Here is another alternative I’ve picked up along the way - don’t reuse controller, but reuse the view:

# Router
get "/users/:id", InternalController, :get_patient

# InternalController
def get_user(conn, %{"id" => id}) do
  conn
  |> put_view(MarkoWeb.UserView)
  |> render("show.json", patient: Users.get!(id))
end

# Tests
Routes.private_internal_path(conn, :get_user, id)

However, this does not work when your controller has actuall business logic in it, since you’d have to copy-pasta it.

scope "/public", MarkoWeb do
    pipe_through :api
    # Routes.user_path(conn, …, …)
    resources "/users", UserController 
end

scope "/private", MarkoWeb, as: :private do
    pipe_through :internal_api
    # Routes.private_internal_path(conn, :fetch_media, …)
    get "/media/:id", InternalController, :fetch_media 
    # Routes.private_user_path(conn, :show, …)
    get "/users/:id", UserController, :show
    # Routes.private_post_path(conn, :show, …) 
    get "/posts/:id", PostController, :show 
    # Routes.private_show_item_path(conn, :show, …)
    get "/items/:id", ItemController, :show, as: :show_item 
end

:as changes the function name on the route helper, not the action.

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.6.2 .

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 an as: :private scope?

Yeah, :as on scopes is used as a prefix on the name.

1 Like

Bingo :slight_smile:

# Router
scope "/private", MarkoWeb, as: :private do
    pipe_through :internal_api
    get "/items/:id", ItemController, :show, as: :show_item 
end

# Tests
Routes.private_show_item_path(conn, :show, id)

Thank you :slight_smile: