aj-foster

aj-foster

Dynamically construct path by action and helper name

Question

Given a route with a known controller action (:library_action) and a known helper name (as: :my_lib), how can I reliably generate the route’s path?

Background

I’m creating a library that integrates with Phoenix. This library provides a controller action MyLib.Controller.library_action/2, and asks consumers to add a route to their application’s router, such as:

get "/user/chosen/path/with/:param", MyLib.Controller, :library_action, as: :my_lib

For flexibility, I require the name of the action (:library_action) and the :as option, but leave the exact path up to the consumer. Furthermore, the consumer may choose to reimplement the :library_action controller action in their own application, ignoring the one provided by the library.

Whether the consumer uses the library-provided implementation or substitutes their own controller, it is necessary for other parts of the library to know the path for this action. In the past, the library could rely on the router’s Helpers module, and use the generated my_lib_path(conn :library_action, ...) function to create the path or URL. However, with Phoenix 1.7, the Helpers module is no longer guaranteed to be generated.

Other Notes

Here’s what we can do already:

  1. Get the current router dynamically from the conn via Phoenix.Controller.router_module/1.
  2. Get a list of known routes via Phoenix.Router.routes/1.
  3. Filter by the helper and plug_opts keys to find a route with the correct action name and :as option.
  4. Get the full path as a string, for example "/user/chosen/path/with/:param".
  5. Manually replace path segments with values, when necessary.

However, this process seems error-prone, especially the manual replacement of path segments.

All suggestions welcome! Thank you.

Most Liked

kip

kip

ex_cldr Core Team

Thanks for pinging this thread back into view. The possibility that helpers generation goes away also impacts localized route generation and localized helpers.

Both @BartOtten’s phoenix_localized_routes and my own ex_cldr_routes use helpers to generate routes at runtime to reflect the locale in effect at the time. This cannot be parsed at compile time because at that time the locale is not known.

Localized routes have the same helper name and same path structure but have path components translated (and may have locale components interpolated).

For that reason alone I hope that Helper generation can have a more definitive future.

dimitarvp

dimitarvp

That’s such an interesting question, shame nobody chimed in.

LostKobrakai

LostKobrakai

Translations are available at compile time, so I feel like it should be possible to figure out the language dependent segments at compile time and then return something akin to this:

# ~x"/pages/#{@page}"
case @locale do
  :en -> ~p"/pages/#{@page}"
  :es -> ~p"/paginas/#{@page}"
  :de -> ~p"/seiten/#{@page}"
end

Last Post!

BartOtten

BartOtten

One of the reasons for generating those mind bending helpers from the original ones was to be future proof as I didn’t have to keep Router code in sync with upstream. Any helper added by the Phoenix team would also automatically be supported by the localization lib. It didn’t age well :slight_smile:

Where Next?

Popular in Questions Top

JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
RisingFromAshes
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
New
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New

Other popular topics Top

nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
AstonJ
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New

We're in Beta

About us Mission Statement