Dynamically call/ build routes without using path helpers

Hello everyone!
I was wondering if it’s possible to call/ build routes without using the named path helpers, something like:

Router.Helpers.path(Endpoint, :page, :show, 1234)
Router.Helpers.path(@conn, :page, :show, 1234)

# Instead of:
# Router.Helpers.page_path(Endpoint, :show, 1234)
# Router.Helpers.page_path(@conn, :show, 1234)

PS.: I’m aware that there’s a path function in the Router.Helpers, but I’m not sure if it’s used for a similar purpose or it’s only internal - I couldn’t find any documentation.

I’m not sure how you’d accomplish your goal right now but wanted to point out that the @moduledoc false at the top of that module indicates that it is a private api and that you should not directly call it because it may break in patch-level updates to the library: https://hexdocs.pm/elixir/writing-documentation.html#hiding-internal-modules-and-functions

1 Like

You can also use apply here to call a path function dynamically:

path = :page_path
apply(Router.Helpers, path, [:show, 1234])

@axelson Yes, I’m aware of that. Thanks for the heads up though :slightly_smiling_face:.

@benwilson512 Nice! I think this will work for me. I guess, having to append the “path” portion is a minor inconvenience, but I think I could do something like this:

path = String.to_existing_atom("#{resource}_path")
apply(Router.Helpers, path, [:show, 1234]) 

Thanks for the help guys, cheers!