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

Skip to content

Commit 1e9c04a

Browse files
committed
Allow to configure a redirection from the route configuration
1 parent cfdc145 commit 1e9c04a

File tree

8 files changed

+107
-1
lines changed

8 files changed

+107
-1
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
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\Component\Routing\Loader;
13+
14+
/**
15+
* Common parsing logic between route loaders.
16+
*
17+
* @author Samuel Roze <[email protected]>
18+
*/
19+
trait LoaderHelper
20+
{
21+
private function redirectToDefaults(string $redirect) : array
22+
{
23+
if ($this->looksLikeUrl($redirect)) {
24+
$defaults['_controller'] = 'Symfony\Bundle\FrameworkBundle\Controller\RedirectController::urlRedirectAction';
25+
$defaults['path'] = $redirect;
26+
} else {
27+
$defaults['_controller'] = 'Symfony\Bundle\FrameworkBundle\Controller\RedirectController::redirectAction';
28+
$defaults['route'] = $redirect;
29+
}
30+
31+
return $defaults;
32+
}
33+
34+
private function looksLikeUrl(string $urlOrRouteName) : bool
35+
{
36+
foreach (['/', '//', 'http://', 'https://'] as $pattern) {
37+
if (substr($urlOrRouteName, 0, strlen($pattern)) == $pattern) {
38+
return true;
39+
}
40+
}
41+
42+
return false;
43+
}
44+
}

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
*/
2626
class XmlFileLoader extends FileLoader
2727
{
28+
use LoaderHelper;
29+
2830
const NAMESPACE_URI = 'http://symfony.com/schema/routing';
2931
const SCHEME_PATH = '/schema/routing/routing-1.0.xsd';
3032

@@ -239,6 +241,10 @@ private function parseConfigs(\DOMElement $node, $path)
239241
$defaults['_controller'] = $controller;
240242
}
241243

244+
if ($redirectTo = $node->getAttribute('redirect-to')) {
245+
$defaults += $this->redirectToDefaults($redirectTo);
246+
}
247+
242248
return array($defaults, $requirements, $options, $condition);
243249
}
244250

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,10 @@
2626
*/
2727
class YamlFileLoader extends FileLoader
2828
{
29+
use LoaderHelper;
30+
2931
private static $availableKeys = array(
30-
'resource', 'type', 'prefix', 'path', 'host', 'schemes', 'methods', 'defaults', 'requirements', 'options', 'condition', 'controller',
32+
'resource', 'type', 'prefix', 'path', 'host', 'schemes', 'methods', 'defaults', 'requirements', 'options', 'condition', 'controller', 'redirect_to',
3133
);
3234
private $yamlParser;
3335

@@ -115,6 +117,10 @@ protected function parseRoute(RouteCollection $collection, $name, array $config,
115117
$methods = isset($config['methods']) ? $config['methods'] : array();
116118
$condition = isset($config['condition']) ? $config['condition'] : null;
117119

120+
if (isset($config['redirect_to'])) {
121+
$defaults += $this->redirectToDefaults($config['redirect_to']);
122+
}
123+
118124
if (isset($config['controller'])) {
119125
$defaults['_controller'] = $config['controller'];
120126
}

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
@@ -42,6 +42,7 @@
4242
<xsd:attribute name="schemes" type="xsd:string" />
4343
<xsd:attribute name="methods" type="xsd:string" />
4444
<xsd:attribute name="controller" type="xsd:string" />
45+
<xsd:attribute name="redirect-to" type="xsd:string" />
4546
</xsd:complexType>
4647

4748
<xsd:complexType name="import">
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?xml version="1.0" encoding="UTF-8" ?>
2+
<routes xmlns="http://symfony.com/schema/routing"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://symfony.com/schema/routing
5+
http://symfony.com/schema/routing/routing-1.0.xsd">
6+
7+
<route id="app_homepage" path="/homepage" controller="AppBundle:Homepage:show" />
8+
<route id="app_entrypoint" path="/" redirect-to="app_homepage" />
9+
<route id="app_temporary_redirect" path="/temporary-home" redirect-to="/homepage" />
10+
</routes>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
app_homepage:
2+
path: /homepage
3+
controller: AppBundle:Homepage:show
4+
5+
app_entrypoint:
6+
path: /
7+
redirect_to: app_homepage
8+
9+
app_temporary_redirect:
10+
path: /temporary-home
11+
redirect_to: /homepage

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,4 +361,18 @@ public function testImportWithOverriddenController()
361361
$loader = new XmlFileLoader(new FileLocator(array(__DIR__.'/../Fixtures/controller')));
362362
$loader->load('import_override_defaults.xml');
363363
}
364+
365+
public function testImportWithRedirections()
366+
{
367+
$loader = new XmlFileLoader(new FileLocator(array(__DIR__.'/../Fixtures/redirect_to')));
368+
$routeCollection = $loader->load('routing.xml');
369+
370+
$route = $routeCollection->get('app_entrypoint');
371+
$this->assertSame('Symfony\Bundle\FrameworkBundle\Controller\RedirectController::redirectAction', $route->getDefault('_controller'));
372+
$this->assertSame('app_homepage', $route->getDefault('route'));
373+
374+
$route = $routeCollection->get('app_temporary_redirect');
375+
$this->assertSame('Symfony\Bundle\FrameworkBundle\Controller\RedirectController::urlRedirectAction', $route->getDefault('_controller'));
376+
$this->assertSame('/homepage', $route->getDefault('path'));
377+
}
364378
}

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,4 +182,18 @@ public function testImportWithOverriddenController()
182182
$loader = new YamlFileLoader(new FileLocator(array(__DIR__.'/../Fixtures/controller')));
183183
$loader->load('import_override_defaults.yml');
184184
}
185+
186+
public function testImportWithRedirections()
187+
{
188+
$loader = new YamlFileLoader(new FileLocator(array(__DIR__.'/../Fixtures/redirect_to')));
189+
$routeCollection = $loader->load('routing.yml');
190+
191+
$route = $routeCollection->get('app_entrypoint');
192+
$this->assertSame('Symfony\Bundle\FrameworkBundle\Controller\RedirectController::redirectAction', $route->getDefault('_controller'));
193+
$this->assertSame('app_homepage', $route->getDefault('route'));
194+
195+
$route = $routeCollection->get('app_temporary_redirect');
196+
$this->assertSame('Symfony\Bundle\FrameworkBundle\Controller\RedirectController::urlRedirectAction', $route->getDefault('_controller'));
197+
$this->assertSame('/homepage', $route->getDefault('path'));
198+
}
185199
}

0 commit comments

Comments
 (0)