Can your import routes that are in separate files into router.ex?


I’m going to migrate from an old site to a new Phoenix site, and have some old routes that are no longer used or need to be redirected.
I’d like to park these redirects in a routes file of it’s own, as these are legacy and I do not want them mixing with the routes of the new site. Is there a way to then “import” this into router.ex so the redirects can be made?

1 Like
defmodule LegacyRoutes do
  defmacro import do
    quote do
      get "/", PageController, :index

And in your router just require LegacyRoutes und call LegacyRoutes.import where you need them.

Depending on the scopes you need the routes in, you might need to split it into more than a single macro and distribute the calls over your scopes.

PS: I never actually tried it, but this is how I would expect it to work.


Depending on how/where you are hosting this, it might make sense to do this at your Load Balancer or API Edge.

1 Like