Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 5fe9639

Browse files
author
Amrouche Hamza
committed
[FrameworkBundle] keep query params in redirection
1 parent 7b8934b commit 5fe9639

File tree

2 files changed

+25
-10
lines changed

2 files changed

+25
-10
lines changed

src/Symfony/Bundle/FrameworkBundle/Controller/RedirectController.php

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -46,26 +46,23 @@ public function __construct(UrlGeneratorInterface $router = null, int $httpPort
4646
* In case the route name is empty, the status code will be 404 when permanent is false
4747
* and 410 otherwise.
4848
*
49-
* @param Request $request The request instance
50-
* @param string $route The route name to redirect to
51-
* @param bool $permanent Whether the redirection is permanent
52-
* @param bool|array $ignoreAttributes Whether to ignore attributes or an array of attributes to ignore
53-
* @param bool $keepRequestMethod Wheter redirect action should keep HTTP request method
49+
* @param bool|array $ignoreAttributes Whether to ignore attributes or an array of attributes to ignore
5450
*
5551
* @throws HttpException In case the route name is empty
5652
*/
57-
public function redirectAction(Request $request, string $route, bool $permanent = false, $ignoreAttributes = false, bool $keepRequestMethod = false): Response
53+
public function redirectAction(Request $request, string $route, bool $permanent = false, $ignoreAttributes = false, bool $keepRequestMethod = false, bool $keepQueryParams = false): Response
5854
{
5955
if ('' == $route) {
6056
throw new HttpException($permanent ? 410 : 404);
6157
}
6258

6359
$attributes = array();
64-
if (false === $ignoreAttributes || is_array($ignoreAttributes)) {
65-
$attributes = $request->attributes->get('_route_params');
60+
if (false === $ignoreAttributes || \is_array($ignoreAttributes)) {
61+
$routeParams = $request->attributes->get('_route_params');
62+
$attributes = $keepQueryParams ? \array_merge($request->query->all(), $routeParams) : $routeParams;
6663
unset($attributes['route'], $attributes['permanent'], $attributes['ignoreAttributes'], $attributes['keepRequestMethod']);
6764
if ($ignoreAttributes) {
68-
$attributes = array_diff_key($attributes, array_flip($ignoreAttributes));
65+
$attributes = \array_diff_key($attributes, \array_flip($ignoreAttributes));
6966
}
7067
}
7168

src/Symfony/Bundle/FrameworkBundle/Tests/Controller/RedirectControllerTest.php

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,6 @@ public function testRoute($permanent, $keepRequestMethod, $ignoreAttributes, $ex
7878
$controller = new RedirectController($router);
7979

8080
$returnResponse = $controller->redirectAction($request, $route, $permanent, $ignoreAttributes, $keepRequestMethod);
81-
8281
$this->assertRedirectUrl($returnResponse, $url);
8382
$this->assertEquals($expectedCode, $returnResponse->getStatusCode());
8483
}
@@ -235,6 +234,25 @@ public function testPathQueryParams($expectedUrl, $path, $queryString)
235234
$this->assertRedirectUrl($returnValue, $expectedUrl);
236235
}
237236

237+
public function testRedirectWithQuery()
238+
{
239+
$scheme = 'http';
240+
$host = 'www.example.com';
241+
$baseUrl = '/base';
242+
$port = 80;
243+
244+
$request = $this->createRequestObject($scheme, $host, $port, $baseUrl, 'base=zaza');
245+
$request->query = new ParameterBag(['base' => 'zaza']);
246+
$request->attributes = new ParameterBag(['_route_params' => []]);
247+
$urlGenerator = $this->getMockBuilder(UrlGeneratorInterface::class)->getMock();
248+
$urlGenerator->expects($this->any())->method('generate')->will($this->returnValue('/test?base=zaza'));
249+
250+
$controller = new RedirectController($urlGenerator);
251+
252+
$returnValue = $controller->redirectAction($request, '/test', false, false, false, true);
253+
$this->assertRedirectUrl($returnValue, '/test?base=zaza');
254+
}
255+
238256
private function createRequestObject($scheme, $host, $port, $baseUrl, $queryString = '')
239257
{
240258
$request = $this->getMockBuilder('Symfony\Component\HttpFoundation\Request')->getMock();

0 commit comments

Comments
 (0)