-
-
Notifications
You must be signed in to change notification settings - Fork 9.7k
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if you call 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(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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, '?')) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
} | ||
|
||
|
@@ -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); | ||
} | ||
|
||
/** | ||
|
There was a problem hiding this comment.
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