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

Skip to content

Commit 859bc4f

Browse files
committed
Add the ability to configure if the redirection is permanent or not
1 parent 1e9c04a commit 859bc4f

File tree

8 files changed

+29
-6
lines changed

8 files changed

+29
-6
lines changed

src/Symfony/Component/Routing/Loader/LoaderHelper.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
*/
1919
trait LoaderHelper
2020
{
21-
private function redirectToDefaults(string $redirect) : array
21+
private function redirectToDefaults(string $redirect, bool $permanent = false): array
2222
{
2323
if ($this->looksLikeUrl($redirect)) {
2424
$defaults['_controller'] = 'Symfony\Bundle\FrameworkBundle\Controller\RedirectController::urlRedirectAction';
@@ -28,12 +28,16 @@ private function redirectToDefaults(string $redirect) : array
2828
$defaults['route'] = $redirect;
2929
}
3030

31+
if ($permanent) {
32+
$defaults['permanent'] = true;
33+
}
34+
3135
return $defaults;
3236
}
3337

34-
private function looksLikeUrl(string $urlOrRouteName) : bool
38+
private function looksLikeUrl(string $urlOrRouteName): bool
3539
{
36-
foreach (['/', '//', 'http://', 'https://'] as $pattern) {
40+
foreach (array('/', '//', 'http://', 'https://') as $pattern) {
3741
if (substr($urlOrRouteName, 0, strlen($pattern)) == $pattern) {
3842
return true;
3943
}

src/Symfony/Component/Routing/Loader/XmlFileLoader.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ private function parseConfigs(\DOMElement $node, $path)
242242
}
243243

244244
if ($redirectTo = $node->getAttribute('redirect-to')) {
245-
$defaults += $this->redirectToDefaults($redirectTo);
245+
$defaults += $this->redirectToDefaults($redirectTo, $node->getAttribute('redirect-permanent'));
246246
}
247247

248248
return array($defaults, $requirements, $options, $condition);

src/Symfony/Component/Routing/Loader/YamlFileLoader.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class YamlFileLoader extends FileLoader
2929
use LoaderHelper;
3030

3131
private static $availableKeys = array(
32-
'resource', 'type', 'prefix', 'path', 'host', 'schemes', 'methods', 'defaults', 'requirements', 'options', 'condition', 'controller', 'redirect_to',
32+
'resource', 'type', 'prefix', 'path', 'host', 'schemes', 'methods', 'defaults', 'requirements', 'options', 'condition', 'controller', 'redirect_to', 'redirect_permanent',
3333
);
3434
private $yamlParser;
3535

@@ -118,7 +118,7 @@ protected function parseRoute(RouteCollection $collection, $name, array $config,
118118
$condition = isset($config['condition']) ? $config['condition'] : null;
119119

120120
if (isset($config['redirect_to'])) {
121-
$defaults += $this->redirectToDefaults($config['redirect_to']);
121+
$defaults += $this->redirectToDefaults($config['redirect_to'], $config['redirect_permanent'] ?? false);
122122
}
123123

124124
if (isset($config['controller'])) {

src/Symfony/Component/Routing/Loader/schema/routing/routing-1.0.xsd

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
<xsd:attribute name="methods" type="xsd:string" />
4444
<xsd:attribute name="controller" type="xsd:string" />
4545
<xsd:attribute name="redirect-to" type="xsd:string" />
46+
<xsd:attribute name="redirect-permanent" type="xsd:boolean" />
4647
</xsd:complexType>
4748

4849
<xsd:complexType name="import">

src/Symfony/Component/Routing/Tests/Fixtures/redirect_to/routing.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@
77
<route id="app_homepage" path="/homepage" controller="AppBundle:Homepage:show" />
88
<route id="app_entrypoint" path="/" redirect-to="app_homepage" />
99
<route id="app_temporary_redirect" path="/temporary-home" redirect-to="/homepage" />
10+
<route id="app_permanent_redirect" path="/permanent-home" redirect-to="/homepage" redirect-permanent="true" />
1011
</routes>

src/Symfony/Component/Routing/Tests/Fixtures/redirect_to/routing.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,8 @@ app_entrypoint:
99
app_temporary_redirect:
1010
path: /temporary-home
1111
redirect_to: /homepage
12+
13+
app_permanent_redirect:
14+
path: /permanent-home
15+
redirect_to: /homepage
16+
redirect_permanent: true

src/Symfony/Component/Routing/Tests/Loader/XmlFileLoaderTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,5 +374,11 @@ public function testImportWithRedirections()
374374
$route = $routeCollection->get('app_temporary_redirect');
375375
$this->assertSame('Symfony\Bundle\FrameworkBundle\Controller\RedirectController::urlRedirectAction', $route->getDefault('_controller'));
376376
$this->assertSame('/homepage', $route->getDefault('path'));
377+
$this->assertFalse($route->hasDefault('permanent'));
378+
379+
$route = $routeCollection->get('app_permanent_redirect');
380+
$this->assertSame('Symfony\Bundle\FrameworkBundle\Controller\RedirectController::urlRedirectAction', $route->getDefault('_controller'));
381+
$this->assertSame('/homepage', $route->getDefault('path'));
382+
$this->assertTrue($route->hasDefault('permanent'));
377383
}
378384
}

src/Symfony/Component/Routing/Tests/Loader/YamlFileLoaderTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,5 +195,11 @@ public function testImportWithRedirections()
195195
$route = $routeCollection->get('app_temporary_redirect');
196196
$this->assertSame('Symfony\Bundle\FrameworkBundle\Controller\RedirectController::urlRedirectAction', $route->getDefault('_controller'));
197197
$this->assertSame('/homepage', $route->getDefault('path'));
198+
$this->assertFalse($route->hasDefault('permanent'));
199+
200+
$route = $routeCollection->get('app_permanent_redirect');
201+
$this->assertSame('Symfony\Bundle\FrameworkBundle\Controller\RedirectController::urlRedirectAction', $route->getDefault('_controller'));
202+
$this->assertSame('/homepage', $route->getDefault('path'));
203+
$this->assertTrue($route->hasDefault('permanent'));
198204
}
199205
}

0 commit comments

Comments
 (0)