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!