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

Skip to content

Commit b6a2af3

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

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,15 +54,16 @@ public function __construct(UrlGeneratorInterface $router = null, int $httpPort
5454
*
5555
* @throws HttpException In case the route name is empty
5656
*/
57-
public function redirectAction(Request $request, string $route, bool $permanent = false, $ignoreAttributes = false, bool $keepRequestMethod = false): Response
57+
public function redirectAction(Request $request, string $route, bool $permanent = false, $ignoreAttributes = false, bool $keepRequestMethod = false, bool $keepQueryParams = false): Response
5858
{
5959
if ('' == $route) {
6060
throw new HttpException($permanent ? 410 : 404);
6161
}
6262

6363
$attributes = array();
6464
if (false === $ignoreAttributes || is_array($ignoreAttributes)) {
65-
$attributes = $request->attributes->get('_route_params');
65+
$routeParams = $request->attributes->get('_route_params');
66+
$attributes = $keepQueryParams ? \array_merge($request->query->all(), $routeParams) : $routeParams;
6667
unset($attributes['route'], $attributes['permanent'], $attributes['ignoreAttributes'], $attributes['keepRequestMethod']);
6768
if ($ignoreAttributes) {
6869
$attributes = array_diff_key($attributes, array_flip($ignoreAttributes));

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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(array('base' => 'zaza'));
247+
$request->attributes = new ParameterBag(array('_route_params' => array()));
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();

0 commit comments

Comments
 (0)