diff --git a/src/Symfony/Bundle/FrameworkBundle/Routing/Router.php b/src/Symfony/Bundle/FrameworkBundle/Routing/Router.php index c0b68a2d17a46..e8cd23f96649d 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Routing/Router.php +++ b/src/Symfony/Bundle/FrameworkBundle/Routing/Router.php @@ -86,7 +86,7 @@ public function warmUp($cacheDir) */ private function resolveParameters(RouteCollection $collection) { - foreach ($collection as $route) { + foreach ($collection as $routeName => $route) { foreach ($route->getDefaults() as $name => $value) { $route->setDefault($name, $this->resolve($value)); } @@ -97,6 +97,18 @@ private function resolveParameters(RouteCollection $collection) } $route->setRequirement($name, $this->resolve($value)); + $default = $route->getDefault($name); + $requirement = $route->getRequirement($name); + + if (null !== $default && 1 !== preg_match('#'.$requirement.'#', $default)) { + throw new RuntimeException(sprintf( + 'Default value "%s" for "%s" does not match requirement "%s" in "%s" route.', + $default, + $name, + $requirement, + $routeName + )); + } } $route->setPath($this->resolve($route->getPath())); diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/Routing/RouterTest.php b/src/Symfony/Bundle/FrameworkBundle/Tests/Routing/RouterTest.php index 330e0659ca144..9c248b567b2c1 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/Routing/RouterTest.php +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/Routing/RouterTest.php @@ -185,13 +185,29 @@ public function testExceptionOnNonStringParameter() $router->getRouteCollection()->get('foo'); } + /** + * @expectedException \Symfony\Component\DependencyInjection\Exception\RuntimeException + * @expectedExceptionMessage Default value "baz" for "bar" does not match requirement "\d+" in "foo" route. + */ + public function testExceptionOnDefaultValueNotMatchingRequirement() + { + $routes = new RouteCollection(); + $routes->add('foo', new Route('foo', array('bar' => 'baz'), array('bar' => '\d+'))); + + $sc = $this->getServiceContainer($routes); + + $router = new Router($sc, 'foo'); + + $route = $router->getRouteCollection()->get('foo'); + } + /** * @dataProvider getNonStringValues */ public function testDefaultValuesAsNonStrings($value) { $routes = new RouteCollection(); - $routes->add('foo', new Route('foo', array('foo' => $value), array('foo' => '\d+'))); + $routes->add('foo', new Route('foo', array('foo' => $value))); $sc = $this->getServiceContainer($routes);