Skip to content

Keep query params on redirect #27482

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ CHANGELOG
4.1.0
-----

* Added optional $keepQueryParams argument to redirect() and redirectToRoute() methods
Copy link
Member

Choose a reason for hiding this comment

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

should be moved in the 4.2 section

* Allowed to pass an optional `LoggerInterface $logger` instance to the `Router`
* Added a new `parameter_bag` service with related autowiring aliases to access parameters as-a-service
* Allowed the `Router` to work with any PSR-11 container
Expand Down
17 changes: 14 additions & 3 deletions src/Symfony/Bundle/FrameworkBundle/Controller/ControllerTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,19 @@ protected function forward(string $controller, array $path = array(), array $que
*
* @final
*/
protected function redirect(string $url, int $status = 302): RedirectResponse
protected function redirect(string $url, int $status = 302, bool $keepQueryParams = false): RedirectResponse
Copy link
Contributor

Choose a reason for hiding this comment

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

As this function accepts a URL and not a route, I don't see the usefulness of this feature to be honest. When generating the URL, you can already use the url generator to add the current query string based on $request->query->all();.

Copy link
Author

@lfjeff lfjeff Jun 4, 2018

Choose a reason for hiding this comment

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

redirect() is called by redirectToRoute() and I wanted to keep things consistent with both functions and with fix #26281. Also, the way I did things it will append the old query params to the redirect url (which might have its own query params). I basically borrowed the code from urlRedirectAction() and gave these functions the same $keepQueryParams option.

I had a bunch of code where I was appending query params before calling redirect() or redirectToRoute() and it just made sense to move the logic into the function.

Copy link
Member

Choose a reason for hiding this comment

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

if you call redirectToRoute, you can pass extra params to the url generator, and it will put them in the query string.

So IMO, this is not needed (the redirect controller does not give you the control of the arguments which is why it needed to add this feature)

{
if ($keepQueryParams) {
$qs = $this->container->get('request_stack')->getCurrentRequest()->getQueryString();
Copy link
Contributor

Choose a reason for hiding this comment

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

maybe you can rename $qs to $queryString ?

if ($qs) {
if (false === strpos($url, '?')) {
Copy link
Contributor

Choose a reason for hiding this comment

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

it will be more readable with a ternary

$url .= '?'.$qs;
} else {
$url .= '&'.$qs;
}
}
}

return new RedirectResponse($url, $status);
}

Expand All @@ -104,9 +115,9 @@ protected function redirect(string $url, int $status = 302): RedirectResponse
*
* @final
*/
protected function redirectToRoute(string $route, array $parameters = array(), int $status = 302): RedirectResponse
protected function redirectToRoute(string $route, array $parameters = array(), int $status = 302, bool $keepQueryParams = false): RedirectResponse
{
return $this->redirect($this->generateUrl($route, $parameters), $status);
return $this->redirect($this->generateUrl($route, $parameters), $status, $keepQueryParams);
}

/**
Expand Down