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

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
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
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
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
asweet-confluent
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
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
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
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
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews