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:
- Get the current router dynamically from the
connviaPhoenix.Controller.router_module/1. - Get a list of known routes via
Phoenix.Router.routes/1. - Filter by the
helperandplug_optskeys to find a route with the correct action name and:asoption. - Get the full path as a string, for example
"/user/chosen/path/with/:param". - 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.
Trending in Questions
Other Trending Topics
Latest Phoenix Threads
Latest on Elixir Forum
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #blog-post
- #ai
- #elixirconf-us
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming











Showing Posts 1 to 10- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
dimitarvp
That’s such an interesting question, shame nobody chimed in.
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:Your library could call this with:
kip
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
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
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
@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
Will be back in a week, but those 7 days might make a difference.
LostKobrakai
I wonder why you need the path helper in the first place.
conn.script_nameshould 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 bothPlug.Routeras well asPhoenix.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
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 functionerror at compile time acts as a signal.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
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.