8000 feature #26213 [FrameworkBundle] Add support to 307/308 HTTP status c… · symfony/symfony@3aa59b6 · GitHub
[go: up one dir, main page]

Skip to content

Commit 3aa59b6

Browse files
committed
feature #26213 [FrameworkBundle] Add support to 307/308 HTTP status codes in RedirectController (ZipoKing)
This PR was squashed before being merged into the 4.1-dev branch (closes #26213). Discussion ---------- [FrameworkBundle] Add support to 307/308 HTTP status codes in RedirectController | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #26171 | License | MIT | Doc PR | With this PR `RedirectController` will allow to create redirections with use of 307/308 HTTP status codes together with 301/302. Related RFC documents: * https://tools.ietf.org/html/rfc7231 * https://tools.ietf.org/html/rfc7538 Commits ------- 64fb5a5 [FrameworkBundle] Add support to 307/308 HTTP status codes in RedirectController
2 parents b1f45b3 + 64fb5a5 commit 3aa59b6

File tree

3 files changed

+49
-21
lines changed

3 files changed

+49
-21
lines changed

src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ CHANGELOG
1010
* Added option in workflow dump command to label graph with a custom label
1111
* Using a `RouterInterface` that does not implement the `WarmableInterface` is deprecated and will not be supported in Symfony 5.0.
1212
* The `RequestDataCollector` class has been deprecated and will be removed in Symfony 5.0. Use the `Symfony\Component\HttpKernel\DataCollector\RequestDataCollector` class instead.
13+
* The `RedirectController` class allows for 307/308 HTTP status codes
1314

1415
4.0.0
1516
-----

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

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,15 @@ 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
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
5354
*
5455
* @throws HttpException In case the route name is empty
5556
*/
56-
public function redirectAction(Request $request, string $route, bool $permanent = false, $ignoreAttributes = false): Response
57+
public fun 8000 ction redirectAction(Request $request, string $route, bool $permanent = false, $ignoreAttributes = false, bool $keepRequestMethod = false): Response
5758
{
5859
if ('' == $route) {
5960
throw new HttpException($permanent ? 410 : 404);
@@ -62,13 +63,19 @@ public function redirectAction(Request $request, string $route, bool $permanent
6263
$attributes = array();
6364
if (false === $ignoreAttributes || is_array($ignoreAttributes)) {
6465
$attributes = $request->attributes->get('_route_params');
65-
unset($attributes['route'], $attributes['permanent'], $attributes['ignoreAttributes']);
66+
unset($attributes['route'], $attributes['permanent'], $attributes['ignoreAttributes'], $attributes['keepRequestMethod']);
6667
if ($ignoreAttributes) {
6768
$attributes = array_diff_key($attributes, array_flip($ignoreAttributes));
6869
}
6970
}
7071

71-
return new RedirectResponse($this->router->generate($route, $attributes, UrlGeneratorInterface::ABSOLUTE_URL), $permanent ? 301 : 302);
72+
if ($keepRequestMethod) {
73+
$statusCode = $permanent ? 308 : 307;
74+
} else {
75+
$statusCode = $permanent ? 301 : 302;
76+
}
77+
78+
return new RedirectResponse($this->router->generate($route, $attributes, UrlGeneratorInterface::ABSOLUTE_URL), $statusCode);
7279
}
7380

7481
/**
@@ -80,22 +87,27 @@ public function redirectAction(Request $request, string $route, bool $permanent
8087
* In case the path is empty, the status code will be 404 when permanent is false
8188
* and 410 otherwise.
8289
*
83-
* @param Request $request The request instance
84-
* @param string $path The absolute path or URL to redirect to
85-
* @param bool $permanent Whether the redirect is permanent or not
86-
* @param string|null $scheme The URL scheme (null to keep the current one)
87-
* @param int|null $httpPort The HTTP port (null to keep the current one for the same scheme or the default configured port)
88-
* @param int|null $httpsPort The HTTPS port (null to keep the current one for the same scheme or the default configured port)
90+
* @param Request $request The request instance
91+
* @param string $path The absolute path or URL to redirect to
92+
* @param bool $permanent Whether the redirect is permanent or not
93+
* @param string|null $scheme The URL scheme (null to keep the current one)
94+
* @param int|null $httpPort The HTTP port (null to keep the current one for the same scheme or the default configured port)
95+
* @param int|null $httpsPort The HTTPS port (null to keep the current one for the same scheme or the default configured port)
96+
* @param bool $keepRequestMethod Wheter redirect action should keep HTTP request method
8997
*
9098
* @throws HttpException In case the path is empty
9199
*/
92-
public function urlRedirectAction(Request $request, string $path, bool $permanent = false, string $scheme = null, int $httpPort = null, int $httpsPort = null): Response
100+
public function urlRedirectAction(Request $request, string $path, bool $permanent = false, string $scheme = null, int $httpPort = null, int $httpsPort = null, bool $keepRequestMethod = false): Response
93101
{
94102
if ('' == $path) {
95103
throw new HttpException($permanent ? 410 : 404);
96104
}
97105

98-
$statusCode = $permanent ? 301 : 302;
106+
if ($keepRequestMethod) {
107+
$statusCode = $permanent ? 308 : 307;
108+
} else {
109+
$statusCode = $permanent ? 301 : 302;
110+
}
99111

100112
// redirect if the path is a full URL
101113
if (parse_url($path, PHP_URL_SCHEME)) {

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

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public function testEmptyRoute()
4747
/**
4848
* @dataProvider provider
4949
*/
50-
public function testRoute($permanent, $ignoreAttributes, $expectedCode, $expectedAttributes)
50+
public function testRoute($permanent, $keepRequestMethod, $ignoreAttributes, $expectedCode, $expectedAttributes)
5151
{
5252
$request = new Request();
5353

@@ -62,6 +62,7 @@ public function testRoute($permanent, $ignoreAttributes, $expectedCode, $expecte
6262
'permanent' => $permanent,
6363
'additional-parameter' => 'value',
6464
'ignoreAttributes' => $ignoreAttributes,
65+
'keepRequestMethod' => $keepRequestMethod,
6566
),
6667
);
6768

@@ -76,7 +77,7 @@ public function testRoute($permanent, $ignoreAttributes, $expectedCode, $expecte
7677

7778
$controller = new RedirectController($router);
7879

79-
$returnResponse = $controller->redirectAction($request, $route, $permanent, $ignoreAttributes);
80+
$returnResponse = $controller->redirectAction($request, $route, $permanent, $ignoreAttributes, $keepRequestMethod);
8081

8182
$this->assertRedirectUrl($returnResponse, $url);
8283
$this->assertEquals($expectedCode, $returnResponse->getStatusCode());
@@ -85,10 +86,14 @@ public function testRoute($permanent, $ignoreAttributes, $expectedCode, $expecte
8586
public function provider()
8687
{
8788
return array(
88-
array(true, false, 301, array('additional-parameter' => 'value')),
89-
array(false, false, 302, array('additional-parameter' => 'value')),
90-
array(false, true, 302, array()),
91-
array(false, array('additional-parameter'), 302, array()),
89+
array(true, false, false, 301, array('additional-parameter' => 'value')),
90+
array(false, false, false, 302, array('additional-parameter' => 'value')),
91+
array(false, false, true, 302, array()),
92+
array(false, false, array('additional-parameter'), 302, array()),
93+
array(true, true, false, 308, array('additional-parameter' => 'value')),
94+
array(false, true, false, 307, array('additional-parameter' => 'value')),
95+
array(false, true, true, 307, array()),
96+
array(false, true, array('additional-parameter'), 307, array()),
9297
);
9398
}
9499

@@ -122,6 +127,16 @@ public function testFullURL()
122127
$this->assertEquals(302, $returnResponse->getStatusCode());
123128
}
124129

130+
public function testFullURLWithMethodKeep()
131+
{
132+
$request = new Request();
133+
$controller = new RedirectController();
134+
$returnResponse = $controller->urlRedirectAction($request, 'http://foo.bar/', false, null, null, null, true);
135+
136+
$this->assertRedirectUrl($returnResponse, 'http://foo.bar/');
137+
$this->assertEquals(307, $returnResponse->getStatusCode());
138+
}
139+
125140
public function testUrlRedirectDefaultPorts()
126141
{
127142
$host = 'www.example.com';

0 commit comments

Comments
 (0)
0