markomitranic

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 :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 !

Showing Posts 1 to 5

markomitranic

markomitranic OP

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.

LostKobrakai

LostKobrakai

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.

markomitranic

markomitranic OP

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

LostKobrakai

LostKobrakai

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

markomitranic

markomitranic OP

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:

— All posts loaded —

Where Next? Top

Trending in Questions Top

Blokh
Hey guys, I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly Do you guys have any suggestions what is the best prac...
New
kszambelanczyk
Hello! Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app. I creat...
New
Onor.io
I have what I’ve heard referred to as a “lookup table” in my database. This is a way of assigning codes to common values. One common lo...
New
Trolleger
What approach to take when sending live updates to “random” users Hi! I have a question, I have a little chat app, and when I create a DM...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
matt-savvy
Anyone here using Honeybadger? My Honeybadger account is being overwhelmed with noise from some bots. Seeing a lot of Bandit.HTTPError...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
wintermeyer
There are three potential reasons for members of this forum to have a look at https://vutuv.de You are tired or annoyed of LinkedIn. Yo...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews