Dylan-aidkr

Dylan-aidkr

Split router while using verified routes

trying to split the large router file

this is some simple representation of my router

# part of MyAppWeb.Router.ex
  scope "/shop/:shop_id" do
    scope "/users" do
      live "/"
      live "/:user_id"
      # large sub routes
      ...
    end

    scope "/items" do
      live "/"
      live "/:item_id"
      # large sub routes
      ...
    end
  end

I searched, and it seems there are 3 options for splitting router.

1. using “forward”

not possible, as it does not allow to forward to route with dynamic segment

== Compilation error in file lib/my_app_web/router.ex ==
** (ArgumentError) dynamic segment "/shop/:shop_id/users" not allowed when forwarding. Use a static path instead

2. using “match”

not able to catch error with verified routes

scope "/shop/:shop_id" do
  match :*, "/users/*path", MyAppWeb.UserRouter, :any
  match :*, "/items/*path", MyAppWeb.ItemRouter, :any
end

with this method invalid path like a “/shop/1234/users/1234/no-exist-path” was not caught by the verified routes.

3. plug routers to Endpoint.ex

not sure if it would work. how to declare verified routes on this setting?

 # part of Endpoint.ex
defmodule MyAppWeb.Endpoint do
...
plug MyAppWeb.Router
plug MyAppWeb.UserRouter
plug MyAppWeb.ItemRouter
end

code above is not working. it seems the Router return NoRouteError before UserRouter.
there is a solution for this(Using multiple routers in Phoenix - #3 by alexgleason)
but still not sure how deal with the VerifiedRoutes.
i need to fix code below to properly handle multiple router.

# part of MyAppWeb.ex
use Phoenix.VerifiedRoutes,
      endpoint: MyAppWeb.Endpoint,
      router: [MyAppWeb.Router, MyAppWeb.UserRouter], # array of router is not valid input
      statics: MyAppWeb.static_paths()

please help!

Marked As Solved

stephane

stephane

Hey @Dylan-aidkr,

One simple solution is to create a macro to split your router.

example

  require MySubRoutes
  ....
# part of MyAppWeb.Router.ex
  scope "/shop/:shop_id" do
    scope "/users" do
      live "/"
      live "/:user_id"
      # large sub routes
      ...
    end

    MySubRoutes.items()
  end
defmodule MySubRoutes do
  defmacro items do
    quote do
       scope "/items" do
         live "/"
         live "/:item_id"
       
         ...
       end
    end
  end
end

This allow you to keep your router clean and forward easily path params.

Also Liked

chrismccord

chrismccord

Creator of Phoenix

You can either use Phoenix.VerifiedRoutes, router: ... in the context where you want to use different routers, or use Phoenix.VerifiedRoutes.path/3 and explicitly pass the router:

  @doc ~S'''
  Generates the router path with route verification.

  See `sigil_p/1` for more information.

  Warns when the provided path does not match against the router specified
  in `use Phoenix.VerifiedRoutes` or the `@router` module attribute.

  ## Examples

      import Phoenix.VerifiedRoutes

      redirect(to: path(conn, MyAppWeb.Router, ~p"/users/top"))

      redirect(to: path(socket, MyAppWeb.Router, ~p"/users/#{@user}"))

      ~H"""
      <.link to={path(@uri, MyAppWeb.Router, ~p"/users?page=#{@page}")}>profile</.link>
      <.link to={path(@uri, MyAppWeb.Router, ~p"/users?#{@params}")}>profile</.link>
      """
  '''
mentero

mentero

We went with

match :*, "/users/*path", MyAppWeb.UserRouter

for router splitting (using macros was causing frequent big recompiles for us)
We are used to alias the Router we want to refer before using path helpers

  alias MyAppWeb.UserRouter.Helpers, as: Routes
  #...
  Routes.users_path(...)

We did not move to verified routes yet, but I would assume we would need to move from aliasing to use Phoenix.VerifiedRoutes

stephane

stephane

You’re right about macro makes it harder to debug / detect errors.

But in this case, your custom macro is just to use Phoenix.(Live).Router macros. Actually the phoenix router contains essentially macros (live, scope, resources…). There are no custom application logic inside yours.

This solution makes it easy to split into smaller files, handle params and upper scopes and verified routes still works :slight_smile:
But very interested if any other ways to solve all of these issues at once.

Where Next?

Popular in Questions Top

marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
JulienCorb
I am trying to implement my new.html.eex file to create new posts on my website. new.html.eex: &lt;h1&gt;Create Post&lt;/h1&gt; &lt;%= ...
New
Lily
In templates/appointment/index.html.eex: &lt;%= for appointment &lt;- @appointments do %&gt; &lt;tr&gt; &lt;td&gt;&lt;%= appoi...
New
beno
I will often find my self writing things similar to: case some_value do nil -&gt; something() "" -&gt; something() _ -&gt; somethi...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
JDanielMartinez
Hi! May someone helps me, please! I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New

Other popular topics Top

sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
985 42920 311
New
Nvim
Anybody knows a comprehensive comparison of Django and Phoenix, thanks for the help. Where are they similar? Where do they differ the m...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 47930 226
New
AstonJ
We’ve put together this wiki for Phoenix LiveView - please feel free to add any info you feel is worth including. What is Phoenix LiveV...
New

We're in Beta

About us Mission Statement