Phoenix router. How to match a query string?

The title says it all really.
The documentation helpfully shows:

user_path(Endpoint, :show, 17, admin: true, active: false)
“/users/17?admin=true&active=false”

Less helpfully it’s not clear to me how to match against query strings in the router. Extensive search hasn’t revealed an answer, so this might help others.

1 Like

Probably because you can’t do that. The router does match on the path, not the query params. You can however match on the params within your controllers or use forward in your router to forward the request to a custom plug, which would route based on your query param.

2 Likes

Thanks Ben @LostKobrakai that helps to know

Oh man I love you so much right now. I’ve been digging into a forwarding issue for hours. I knew I could forward to a router, but I didn’t realize I could just forward to my own custom plug (even though it says so in the docs :man_facepalming:).

Once I was in the plug, I could match on the headers needed, then forward the request to the correct router with MyApp.SomeRouter.call(conn, []).