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

Skip to content

Commit a400739

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

File tree

3 files changed

+29
-13
lines changed

3 files changed

+29
-13
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: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Symfony\Bundle\FrameworkBundle\Tests\Controller;
1313

14+
use phpDocumentor\Reflection\DocBlock\Tags\Param;
1415
use Symfony\Component\HttpFoundation\Response;
1516
use Symfony\Component\HttpFoundation\ParameterBag;
1617
use Symfony\Component\HttpFoundation\Request;
@@ -78,7 +79,6 @@ public function testRoute($permanent, $keepRequestMethod, $ignoreAttributes, $ex
7879
$controller = new RedirectController($router);
7980

8081
$returnResponse = $controller->redirectAction($request, $route, $permanent, $ignoreAttributes, $keepRequestMethod);
81-
8282
$this->assertRedirectUrl($returnResponse, $url);
8383
$this->assertEquals($expectedCode, $returnResponse->getStatusCode());
8484
}
@@ -218,8 +218,8 @@ public function pathQueryParamsProvider()
218218
}
219219

220220
/**
221-
* @dataProvider pathQueryParamsProvider
222-
*/
221+
* @dataProvider pathQueryParamsProvider
222+
*/
223223
public function testPathQueryParams($expectedUrl, $path, $queryString)
224224
{
225225
$scheme = 'http';
@@ -235,6 +235,25 @@ public function testPathQueryParams($expectedUrl, $path, $queryString)
235235
$this->assertRedirectUrl($returnValue, $expectedUrl);
236236
}
237237

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

src/Symfony/Component/Serializer/Normalizer/ObjectNormalizer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<?php
1+
<?php
22

33
/*
44
* This file is part of the Symfony package.

0 commit comments

Comments
 (0)