Skip to content

Commit 62e7de8

Browse files
committed
Make sure the controller & redirect_to configurations conflict with a correct error message
1 parent 3c4ccbe commit 62e7de8

File tree

6 files changed

+40
-0
lines changed

6 files changed

+40
-0
lines changed

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

+4
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,10 @@ private function parseConfigs(\DOMElement $node, $path)
244244
}
245245

246246
if ($redirectTo = $node->getAttribute('redirect-to')) {
247+
if (isset($defaults['_controller'])) {
248+
throw new \InvalidArgumentException(sprintf('The routing file "%s" must not specify both a controller and a redirection.', $path));
249+
}
250+
247251
$defaults['_redirect_to'] = $redirectTo;
248252
$defaults['_redirect_permanent'] = (bool) $node->getAttribute('redirect-permanent');
249253
}

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

+4
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,10 @@ protected function parseRoute(RouteCollection $collection, $name, array $config,
120120
}
121121

122122
if (isset($config['redirect_to'])) {
123+
if (isset($defaults['_controller'])) {
124+
throw new \InvalidArgumentException(sprintf('The routing file "%s" must not specify both a controller and a redirection.', $path));
125+
}
126+
123127
$defaults['_redirect_to'] = $config['redirect_to'];
124128
$defaults['_redirect_permanent'] = $config['redirect_permanent'] ?? false;
125129
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
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_symfony" path="/symfony" redirect-to="https://symfony.com" controller="App\Controller\Symfony" />
8+
</routes>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
app_symfony:
2+
path: /symfony
3+
controller: App\Controller\Symfony
4+
redirect_to: https://symfony.com

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

+10
Original file line numberDiff line numberDiff line change
@@ -389,4 +389,14 @@ public function testRedirections()
389389
$this->assertSame('/', $route->getDefault('_redirect_to'));
390390
$this->assertTrue($route->getDefault('_redirect_permanent'));
391391
}
392+
393+
/**
394+
* @expectedException \InvalidArgumentException
395+
* @expectedExceptionMessageRegExp /The routing file "[^"]*" must not specify both a controller and a redirection./
396+
*/
397+
public function testRedirectionCannotBeUsedWithController()
398+
{
399+
$loader = new XmlFileLoader(new FileLocator(array(__DIR__.'/../Fixtures/redirect_to')));
400+
$loader->load('redirect_with_controller.xml');
401+
}
392402
}

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

+10
Original file line numberDiff line numberDiff line change
@@ -210,4 +210,14 @@ public function testRedirections()
210210
$this->assertSame('/', $route->getDefault('_redirect_to'));
211211
$this->assertTrue($route->getDefault('_redirect_permanent'));
212212
}
213+
214+
/**
215+
* @expectedException \InvalidArgumentException
216+
* @expectedExceptionMessageRegExp /The routing file "[^"]*" must not specify both a controller and a redirection./
217+
*/
218+
public function testRedirectionCannotBeUsedWithController()
219+
{
220+
$loader = new YamlFileLoader(new FileLocator(array(__DIR__.'/../Fixtures/redirect_to')));
221+
$loader->load('redirect_with_controller.yml');
222+
}
213223
}

0 commit comments

Comments
 (0)