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

Skip to content

Commit 7b20b8c

Browse files
committed
feature #26281 [FrameworkBundle] keep query in redirect (Simperfit)
This PR was merged into the 4.1-dev branch. Discussion ---------- [FrameworkBundle] keep query in redirect | Q | A | ------------- | --- | Branch? | 4.1 | Bug fix? | no | New feature? | yes <!-- don't forget to update src/**/CHANGELOG.md files --> | BC breaks? | no <!-- see https://symfony.com/bc --> | Deprecations? | no <!-- don't forget to update UPGRADE-*.md files --> | Tests pass? | yes <!-- please add some, will be required by reviewers --> | Fixed tickets | #26256 <!-- #-prefixed issue number(s), if any --> | License | MIT | Doc PR | #9314 <!-- Write a short README entry for your feature/bugfix here (replace this comment block.) This will help people understand your PR and can be used as a start of the Doc PR. Additionally: - Bug fixes must be submitted against the lowest branch where they apply (lowest branches are regularly merged to upper ones so they get the fixes too). - Features and deprecations must be submitted against the master branch. --> Commits ------- 406bfc9 [FrameworkBundle] keep query params in redirection
2 parents a949484 + 406bfc9 commit 7b20b8c

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ 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);
@@ -63,6 +63,7 @@ public function redirectAction(Request $request, string $route, bool $permanent
6363
$attributes = array();
6464
if (false === $ignoreAttributes || is_array($ignoreAttributes)) {
6565
$attributes = $request->attributes->get('_route_params');
66+
$attributes = $keepQueryParams ? array_merge($request->query->all(), $attributes) : $attributes;
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: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,40 @@ 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('base2' => 'zaza')));
248+
$urlGenerator = $this->getMockBuilder(UrlGeneratorInterface::class)->getMock();
249+
$urlGenerator->expects($this->once())->method('generate')->will($this->returnValue('/test?base=zaza&base2=zaza'))->with('/test', array('base' => 'zaza', 'base2' => 'zaza'), UrlGeneratorInterface::ABSOLUTE_URL);
250+
251+
$controller = new RedirectController($urlGenerator);
252+
$this->assertRedirectUrl($controller->redirectAction($request, '/test', false, false, false, true), '/test?base=zaza&base2=zaza');
253+
}
254+
255+
public function testRedirectWithQueryWithRouteParamsOveriding()
256+
{
257+
$scheme = 'http';
258+
$host = 'www.example.com';
259+
$baseUrl = '/base';
260+
$port = 80;
261+
262+
$request = $this->createRequestObject($scheme, $host, $port, $baseUrl, 'base=zaza');
263+
$request->query = new ParameterBag(array('base' => 'zaza'));
264+
$request->attributes = new ParameterBag(array('_route_params' => array('base' => 'zouzou')));
265+
$urlGenerator = $this->getMockBuilder(UrlGeneratorInterface::class)->getMock();
266+
$urlGenerator->expects($this->once())->method('generate')->will($this->returnValue('/test?base=zouzou'))->with('/test', array('base' => 'zouzou'), UrlGeneratorInterface::ABSOLUTE_URL);
267+
268+
$controller = new RedirectController($urlGenerator);
269+
$this->assertRedirectUrl($controller->redirectAction($request, '/test', false, false, false, true), '/test?base=zouzou');
270+
}
271+
238272
private function createRequestObject($scheme, $host, $port, $baseUrl, $queryString = '')
239273
{
240274
$request = $this->getMockBuilder('Symfony\Component\HttpFoundation\Request')->getMock();

0 commit comments

Comments
 (0)