feat: add query parameter support to the reverse! macro - #642
Conversation
Path parameters are passed right after the view name, and query parameters can now be added after a semicolon. They get appended to the generated URL as a percent-encoded query string, so you don't have to build it by hand anymore.
ElijahAhianyo
left a comment
There was a problem hiding this comment.
Thanks for the contribution @ChrisJr404! Aside the comment on replicating this for the reverse_redirect! macro, this looks good to me.
| #[macro_export] | ||
| macro_rules! reverse { | ||
| ($request:expr, $view_name:literal $(, $($key:ident = $value:expr),*)?) => {{ | ||
| ($request:expr, $view_name:literal |
There was a problem hiding this comment.
would be nice to have this for reverse_redirect! as well
| /// let url = reverse!(request, "my_custom_app:home")?; | ||
| /// | ||
| /// // with query parameters, this returns `/?page=2&search=cot`: | ||
| /// let url = reverse!(request, "home"; page = 2, search = "cot")?; |
There was a problem hiding this comment.
I'm not really sold on this API. Semicolon make it feel like an end of a statement, and it's also visually difficult to distinct between route params and query params. Maybe we should do something like this instead?
| /// let url = reverse!(request, "home"; page = 2, search = "cot")?; | |
| /// let url = reverse!(request, "home", query: { page = 2, search = "cot" })?; |
What do you think? The question also goes to @seqre @ElijahAhianyo
|
I like the |
Related issue or discussion
Closes #430
Description
Adds query parameter support to
reverse!. Path params are still passed right after the view name, and query params can now be added after a;, using the samekey = valuesyntax:They get appended as a percent-encoded query string (built with
form_urlencoded, which we already depend on). Values only need to implementToString, just like path params do. When no query params are given the URL is returned unchanged, so existingreverse!calls behave exactly as before.Type of change
Checklist
just test-all)just clippy)cargo fmt)