Programmatic access to Phoenix Routes

metaprogramming
phoenix
router

#1

Is it possible to have programmatic access to Phoenix Routes? We are using an instance of a Phoenix.Router to generate in-app links (think myapp://users/42) and I would like to create an admin page that’ll show all the available routes (and generate them with the given ids) but I don’t see any way to introspect the routes. Is it possible? It looks like I could poke in MyApp.AppLinkRouter.__routes__ but that relies on Phoenix internals, so I’d prefer to use a different method.


#2

I checked to see what mix phx.routes uses and it uses __routes__, so I suspect that’s the only option.


#3

There is no public API for this today, but I can see how it could be valuable. In this specific case, it sounds like it would be better to use the existing route helpers given that you have a list of resources or ids? For example:

<%= for user <- @users do %>
  <%= link "user #{user.name}", to: Routes.user_path(@conn, :show, user) %>
<% end %>

Is the discoverability of routes necessary in this case, specifically since you have “(and generate them with the given ids” as part of the requirements? If you have ids, they belong to some resource or subset of resources, which means you need to know up from which routes they belong to, which means programmatic access won’t gain us anything in this case? I could be missing the full requirements, so let me know if I’m off base :slight_smile:


#4

The discoverability of the routes is the main point in this case. Basically I want to render a page (for admins-only) that a similar graphical representation to mix phx.routes (the generation of example concrete routes would just be a bonus).