[Routing] removed restriction of route names

This commit is contained in:
Tobias Schultze 2012-11-27 20:35:04 +01:00 committed by Fabien Potencier
parent fae3e35ef5
commit 828c95d750
7 changed files with 30 additions and 16 deletions

View File

@ -20,6 +20,8 @@ CHANGELOG
check on URL generation completely by calling `setStrictRequirements(null)`. It check on URL generation completely by calling `setStrictRequirements(null)`. It
improves performance in production environment as you should know that params always improves performance in production environment as you should know that params always
pass the requirements (otherwise it would break your link anyway). pass the requirements (otherwise it would break your link anyway).
* There is no restriction on the route name anymore. So non-alphanumeric characters
are now also allowed.
2.1.0 2.1.0
----- -----

View File

@ -112,16 +112,10 @@ class RouteCollection implements \IteratorAggregate, \Countable
* @param string $name The route name * @param string $name The route name
* @param Route $route A Route instance * @param Route $route A Route instance
* *
* @throws \InvalidArgumentException When route name contains non valid characters
*
* @api * @api
*/ */
public function add($name, Route $route) public function add($name, Route $route)
{ {
if (!preg_match('/^[a-z0-9A-Z_.]+$/', $name)) {
throw new \InvalidArgumentException(sprintf('The provided route name "%s" contains non valid characters. A route name must only contain digits (0-9), letters (a-z and A-Z), underscores (_) and dots (.).', $name));
}
$this->remove($name); $this->remove($name);
$this->routes[$name] = $route; $this->routes[$name] = $route;

View File

@ -0,0 +1,2 @@
"#$péß^a|":
pattern: "true"

View File

@ -294,6 +294,13 @@ class UrlGeneratorTest extends \PHPUnit_Framework_TestCase
$this->assertSame('/app.php/test', $this->getGenerator($routes)->generate('test')); $this->assertSame('/app.php/test', $this->getGenerator($routes)->generate('test'));
} }
public function testGenerateWithSpecialRouteName()
{
$routes = $this->getRoutes('$péß^a|', new Route('/bar'));
$this->assertSame('/app.php/bar', $this->getGenerator($routes)->generate('$péß^a|'));
}
public function testUrlEncoding() public function testUrlEncoding()
{ {
// This tests the encoding of reserved characters that are used for delimiting of URI components (defined in RFC 3986) // This tests the encoding of reserved characters that are used for delimiting of URI components (defined in RFC 3986)

View File

@ -79,6 +79,16 @@ class YamlFileLoaderTest extends \PHPUnit_Framework_TestCase
$loader->load('incomplete.yml'); $loader->load('incomplete.yml');
} }
public function testLoadSpecialRouteName()
{
$loader = new YamlFileLoader(new FileLocator(array(__DIR__.'/../Fixtures')));
$routeCollection = $loader->load('special_route_name.yml');
$route = $routeCollection->get('#$péß^a|');
$this->assertInstanceOf('Symfony\Component\Routing\Route', $route);
$this->assertSame('/true', $route->getPattern());
}
public function testLoadWithPattern() public function testLoadWithPattern()
{ {
$loader = new YamlFileLoader(new FileLocator(array(__DIR__.'/../Fixtures'))); $loader = new YamlFileLoader(new FileLocator(array(__DIR__.'/../Fixtures')));

View File

@ -158,6 +158,15 @@ class UrlMatcherTest extends \PHPUnit_Framework_TestCase
$this->assertEquals(array('_locale' => 'fr', '_route' => 'foo', 'foo' => 'foo'), $matcher->match('/fr/b/foo')); $this->assertEquals(array('_locale' => 'fr', '_route' => 'foo', 'foo' => 'foo'), $matcher->match('/fr/b/foo'));
} }
public function testMatchSpecialRouteName()
{
$collection = new RouteCollection();
$collection->add('$péß^a|', new Route('/bar'));
$matcher = new UrlMatcher($collection, new RequestContext());
$this->assertEquals(array('_route' => '$péß^a|'), $matcher->match('/bar'));
}
public function testMatchNonAlpha() public function testMatchNonAlpha()
{ {
$collection = new RouteCollection(); $collection = new RouteCollection();

View File

@ -27,16 +27,6 @@ class RouteCollectionTest extends \PHPUnit_Framework_TestCase
$this->assertNull($collection->get('bar'), '->get() returns null if a route does not exist'); $this->assertNull($collection->get('bar'), '->get() returns null if a route does not exist');
} }
/**
* @expectedException \InvalidArgumentException
*/
public function testAddInvalidRoute()
{
$collection = new RouteCollection();
$route = new Route('/foo');
$collection->add('f o o', $route);
}
public function testOverriddenRoute() public function testOverriddenRoute()
{ {
$collection = new RouteCollection(); $collection = new RouteCollection();