Skip to content

Commit 81b312d

Browse files
bug #53924 [FrameworkBundle] Check if the _route attribute exists on the request (xvilo)
This PR was submitted for the 6.4 branch but it was squashed and merged into the 5.4 branch instead. Discussion ---------- [FrameworkBundle] Check if the _route attribute exists on the request | Q | A | ------------- | --- | Branch? | 5.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | License | MIT The _route attribute does not always exist in some codebases when RedirectController is used. `$request->attributes->get()` will default to `null` if the value does not exist, yielding a PHP error as the return type is set to string. ``` Symfony\Bundle\FrameworkBundle\DataCollector\RouterDataCollector::guessRoute(): Return value must be of type string, null returned ``` Commits ------- 8a615b3 [FrameworkBundle] Check if the _route attribute exists on the request
2 parents c873894 + 8a615b3 commit 81b312d

File tree

2 files changed

+67
-1
lines changed

2 files changed

+67
-1
lines changed

src/Symfony/Bundle/FrameworkBundle/DataCollector/RouterDataCollector.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public function guessRoute(Request $request, $controller)
2828
$controller = $controller[0];
2929
}
3030

31-
if ($controller instanceof RedirectController) {
31+
if ($controller instanceof RedirectController && $request->attributes->has('_route')) {
3232
return $request->attributes->get('_route');
3333
}
3434

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bundle\FrameworkBundle\Tests\DataCollector;
13+
14+
use Symfony\Bundle\FrameworkBundle\Controller\RedirectController;
15+
use Symfony\Bundle\FrameworkBundle\DataCollector\RouterDataCollector;
16+
use Symfony\Bundle\FrameworkBundle\Tests\TestCase;
17+
use Symfony\Component\HttpFoundation\RedirectResponse;
18+
use Symfony\Component\HttpFoundation\Request;
19+
use Symfony\Component\HttpKernel\Event\ControllerEvent;
20+
use Symfony\Component\HttpKernel\KernelInterface;
21+
22+
class RouterDataCollectorTest extends TestCase
23+
{
24+
public function testRouteRedirectControllerNoRouteAtrribute()
25+
{
26+
$collector = new RouterDataCollector();
27+
28+
$request = Request::create('http://test.com/foo?bar=baz');
29+
$response = new RedirectResponse('http://test.com/redirect');
30+
31+
$event = $this->createControllerEvent($request);
32+
33+
$collector->onKernelController($event);
34+
$collector->collect($request, $response);
35+
36+
$this->assertTrue($collector->getRedirect());
37+
$this->assertEquals('http://test.com/redirect', $collector->getTargetUrl());
38+
$this->assertEquals('n/a', $collector->getTargetRoute());
39+
}
40+
41+
public function testRouteRedirectControllerWithRouteAttribute()
42+
{
43+
$collector = new RouterDataCollector();
44+
45+
$request = Request::create('http://test.com/foo?bar=baz');
46+
$request->attributes->set('_route', 'current-route');
47+
48+
$response = new RedirectResponse('http://test.com/redirect');
49+
50+
$event = $this->createControllerEvent($request);
51+
52+
$collector->onKernelController($event);
53+
$collector->collect($request, $response);
54+
55+
$this->assertTrue($collector->getRedirect());
56+
$this->assertEquals('http://test.com/redirect', $collector->getTargetUrl());
57+
$this->assertEquals('current-route', $collector->getTargetRoute());
58+
}
59+
60+
protected function createControllerEvent(Request $request): ControllerEvent
61+
{
62+
$kernel = $this->createMock(KernelInterface::class);
63+
64+
return new ControllerEvent($kernel, new RedirectController(), $request, null);
65+
}
66+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy