Skip to content

Commit 8252822

Browse files
committed
[FrameworkBundle] Thrown an HttpException instead returning a Response in order to show an error page when path is not defined.
1 parent 6e93ecb commit 8252822

File tree

2 files changed

+32
-14
lines changed

2 files changed

+32
-14
lines changed

Controller/RedirectController.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Symfony\Component\HttpFoundation\RedirectResponse;
1616
use Symfony\Component\HttpFoundation\Request;
1717
use Symfony\Component\HttpFoundation\Response;
18+
use Symfony\Component\HttpKernel\Exception\HttpException;
1819
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
1920

2021
/**
@@ -39,11 +40,13 @@ class RedirectController extends ContainerAware
3940
* @param Boolean|array $ignoreAttributes Whether to ignore attributes or an array of attributes to ignore
4041
*
4142
* @return Response A Response instance
43+
*
44+
* @throws HttpException In case the route name is empty
4245
*/
4346
public function redirectAction(Request $request, $route, $permanent = false, $ignoreAttributes = false)
4447
{
4548
if ('' == $route) {
46-
return new Response(null, $permanent ? 410 : 404);
49+
throw new HttpException($permanent ? 410 : 404);
4750
}
4851

4952
$attributes = array();
@@ -75,11 +78,13 @@ public function redirectAction(Request $request, $route, $permanent = false, $ig
7578
* @param integer|null $httpsPort The HTTPS port (null to keep the current one for the same scheme or the configured port in the container)
7679
*
7780
* @return Response A Response instance
81+
*
82+
* @throws HttpException In case the path is empty
7883
*/
7984
public function urlRedirectAction(Request $request, $path, $permanent = false, $scheme = null, $httpPort = null, $httpsPort = null)
8085
{
8186
if ('' == $path) {
82-
return new Response(null, $permanent ? 410 : 404);
87+
throw new HttpException($permanent ? 410 : 404);
8388
}
8489

8590
$statusCode = $permanent ? 301 : 302;

Tests/Controller/RedirectControllerTest.php

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Symfony\Component\HttpFoundation\Response;
1515
use Symfony\Component\HttpFoundation\ParameterBag;
1616
use Symfony\Component\HttpFoundation\Request;
17+
use Symfony\Component\HttpKernel\Exception\HttpException;
1718
use Symfony\Bundle\FrameworkBundle\Controller\RedirectController;
1819
use Symfony\Bundle\FrameworkBundle\Tests\TestCase;
1920

@@ -27,13 +28,19 @@ public function testEmptyRoute()
2728
$request = new Request();
2829
$controller = new RedirectController();
2930

30-
$returnResponse = $controller->redirectAction($request, '', true);
31-
$this->assertInstanceOf('\Symfony\Component\HttpFoundation\Response', $returnResponse);
32-
$this->assertEquals(410, $returnResponse->getStatusCode());
31+
try {
32+
$controller->redirectAction($request, '', true);
33+
$this->fail('Expected Symfony\Component\HttpKernel\Exception\HttpException to be thrown');
34+
} catch (HttpException $e) {
35+
$this->assertSame(410, $e->getStatusCode());
36+
}
3337

34-
$returnResponse = $controller->redirectAction($request, '', false);
35-
$this->assertInstanceOf('\Symfony\Component\HttpFoundation\Response', $returnResponse);
36-
$this->assertEquals(404, $returnResponse->getStatusCode());
38+
try {
39+
$controller->redirectAction($request, '', false);
40+
$this->fail('Expected Symfony\Component\HttpKernel\Exception\HttpException to be thrown');
41+
} catch (HttpException $e) {
42+
$this->assertSame(404, $e->getStatusCode());
43+
}
3744
}
3845

3946
/**
@@ -98,13 +105,19 @@ public function testEmptyPath()
98105
$request = new Request();
99106
$controller = new RedirectController();
100107

101-
$returnResponse = $controller->urlRedirectAction($request, '', true);
102-
$this->assertInstanceOf('\Symfony\Component\HttpFoundation\Response', $returnResponse);
103-
$this->assertEquals(410, $returnResponse->getStatusCode());
108+
try {
109+
$controller->urlRedirectAction($request, '', true);
110+
$this->fail('Expected Symfony\Component\HttpKernel\Exception\HttpException to be thrown');
111+
} catch (HttpException $e) {
112+
$this->assertSame(410, $e->getStatusCode());
113+
}
104114

105-
$returnResponse = $controller->urlRedirectAction($request, '', false);
106-
$this->assertInstanceOf('\Symfony\Component\HttpFoundation\Response', $returnResponse);
107-
$this->assertEquals(404, $returnResponse->getStatusCode());
115+
try {
116+
$controller->urlRedirectAction($request, '', false);
117+
$this->fail('Expected Symfony\Component\HttpKernel\Exception\HttpException to be thrown');
118+
} catch (HttpException $e) {
119+
$this->assertSame(404, $e->getStatusCode());
120+
}
108121
}
109122

110123
public function testFullURL()

0 commit comments

Comments
 (0)