aj-foster

aj-foster

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.

Showing Posts 1 to 10

dimitarvp

dimitarvp

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

al2o3cr

al2o3cr

A more-explicit approach would be to ask users to configure your library with a {module, function, args} tuple that can generate the route.

For instance, you might configure {MyAppWeb.LibHelper, :my_lib_route, [:extra1, :extra2]} and define:

defmodule MyAppWeb.LibHelper do
  def my_lib_route(conn, params, _extra1, _extra2) do
    # extra1 and extra2 are just to show what happens
    MyAppWeb.my_lib_path(conn, :library_action, params)
  end
end

Your library could call this with:

def call_route({mod, fun, args}, conn, params) do
 apply(mod, fun, [conn, params] ++ args)
end
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.

aj-foster

aj-foster OP

I like this approach. There are a few places in the library where I plan to do this or something very similar. Especially if I can find a way to make the existing route helpers module meet the contract as well.

aj-foster

aj-foster OP

I certainly agree. Verified Routes is a powerful addition to Phoenix, and — just to be clear — I fully support it as the default for consumers. Maybe there is a different mechanism that would support 3rd-party libraries like the route helpers do now. Perhaps something with a bit less metaprogramming magic (since we wouldn’t be optimizing for the ergonomics of calling a helper function with exactly the right number of arguments). Not sure what that might look like, but the intersection of our use-cases is quite interesting.

BartOtten

BartOtten

@kip do you have time to talk about it with Phoenix Team? I am living without digital devices as much as possible and the network is really bad over here :slight_smile: Will be back in a week, but those 7 days might make a difference.

LostKobrakai

LostKobrakai

I wonder why you need the path helper in the first place. conn.script_name should give you the segments of the path, which was used to get to your controller. No need to consult the router or anything. Should work fine with both Plug.Router as well as Phoenix.Router. You can use that to build the path within the code you control. If the user wants to link to your controller they know the necessary details and can just use the normal phoenix tools (helpers, verified routes, …) to do so.

kip

kip

ex_cldr Core Team

It not really about knowing the path that got to the current controller. It’s generating paths and URLs to other parts of the site, where those paths are localised. Since they are localised the path cannot be validated at compile time (or at least I can’t think of a way). Using helpers give some minimal guarantee since an unknown function error at compile time acts as a signal.

LostKobrakai

LostKobrakai

I guess my question was more targeted at @aj-foster’s usecase, which seems to be related to urls to itself, rather than other pages.

aj-foster

aj-foster OP

Apologies for not being clear. In reality this library has multiple controller actions. I don’t need the path to the current controller action, but rather the path to another controller action also prescribed by the library. Hence the need to look up the action by name/helper.

Where Next? Top

Trending in Questions Top

stjefim
Hello! Suppose you are building workflow (order / task / payment) processing system with the following requirements: Each workflow con...
New
water
I’m aware of 2 such projects, are there others? git: GitHub - team-alembic/realworld: A fullstack Phoenix LiveView application with back...
New
peck
Hi everyone. My team and I have been working on a fairly modest app based around video streaming and chat, but we’ve landed a customer t...
New
bradley
I really like the adapter patterns that ecto, nebulex, waffle, etc. use and would love find something similar for a key management servic...
New
unaware8150
Hello folks! So at work, we are seeing some situations where we have to define some “fixed” strings that are used across the codebase in...
New
Alvinkariuki
How Can I Optimise Compile Time Dependencies I have been building an elixir application for about 2 years now. Many modules and files ha...
New
leoschweizer
Hi, we run different LLM models across a cluster, with each node loading a configurable subset. Model-specific queues let nodes consume ...
New

Other Trending Topics Top

mudasobwa
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
KristerV
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
juhalehtonen
There has been a thread to discuss the Stack Overflow Developer Survey on this forum every year since 2018, so here’s yet another one for...
New
webofbits
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself. My main conc...
#ai
New
budgie
I love Elixir. It’s one of 2 programming languages I’ve ever fallen in love with. But I don’t use it anymore. Serverless was the promis...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews