Forwarded route with scoped paths

Hello, I have an main application router, which routes to different contexts, ie:

scope "/users/:id", as: user do
  scope "/events", as: event do
    use Event.Router
  end

  scope "/contacts", as: :contact do
    use Contact.Router
  end
end

and each of contexts’ router has a defined macro like this:

defmacro __using__() do
  quote do
    scope "/", Event do
      get("/", EventController, :index)
    end
  end
end

And right now, I’m trying to switch this to https://hexdocs.pm/phoenix/Phoenix.Router.html#forward/4
So I did this in main router:

scope "/users/:id", as: user do
  scope "/event", as: :event do
    forward "/event", Event.Router
  end
end

But with router as Event.Router is shown, it doesn’t work, because it won’t catch the :id from the parent router.

At first, I’ve tried this:

# Event.Router
 scope "/users/:id", Event do
   get("/", EventController, :index)
 end

But then it shows me this error:

** (Phoenix.Router.NoRouteError) no route found for GET /events (Event.Router)

and having this, fixes the error, but I don’t think this should be the solution:

# Event.Router
 get("/events", EventController, :index)
 scope "/users/:id", Event do
   get("/events", EventController, :index)
 end

What should be correct approach here?

1 Like

I’ve been curious in this too, would simplify things, right now I’m parsing the path after the fact, which is not very clean.