Skip to content

Add host constraints to mix phx.routes output - #6594

Open
t3chw wants to merge 1 commit into
phoenixframework:mainfrom
t3chw:add-host-constraints-to-phx-routes
Open

Add host constraints to mix phx.routes output#6594
t3chw wants to merge 1 commit into
phoenixframework:mainfrom
t3chw:add-host-constraints-to-phx-routes

Conversation

@t3chw

@t3chw t3chw commented Feb 8, 2026

Copy link
Copy Markdown

Fixes #6589

This adds host constraints to the mix phx.routes output.

Routes defined inside scope blocks with a host: option are now prefixed
with the corresponding host, making it easier to distinguish routes that share
the same path but differ by host.

Example:

Before:
GET /users MyAppWeb.UserController :index
GET /users MyAppWeb.AdminUserController :index

After:
[api.example.com] GET /users MyAppWeb.UserController :index
[admin.example.com] GET /users MyAppWeb.AdminUserController :index

@SteffenDE
SteffenDE requested a review from josevalim February 21, 2026 15:52
Comment thread lib/phoenix/router.ex
Map.put(route, :label, nested_route.label)
route
|> Map.put(:label, nested_route.label)
|> Map.put(:hosts, Map.get(nested_route, :hosts, route.hosts))

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we need a test for this part of the code.

In particular, if both the parent and the child router define a host, what would happen? We would probably check the host in both cases, so it is not like one would win over the other?

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@t3chw I think @josevalim 's comment is valid: as it is, when both the parent and the child router define a host, only the child's host is shown. I think that's misleading because matching enforces both constraints (parent AND child must hold).

I've made a PR to fix this: t3chw#1

Let me know if the approach makes sense.

dpflucas added a commit to dpflucas/phoenix that referenced this pull request Aug 27, 2026
Addresses review feedback on phoenixframework#6594: when a route is forwarded, host
constraints from both the parent and child router must hold, so combine
them instead of letting the child hosts win. Previously a child with an
empty hosts list also caused the parent host to be dropped.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add host constraints to mix phx.routes output

4 participants