[Routing] moved from :var to {var}

This follows the "URI template" notation:

http://code.google.com/p/uri-templates/
http://tools.ietf.org/html/draft-gregorio-uritemplate-04

You need to change all your route definitions from something like:

    /article/:id

to something like:

    /article/{id}
This commit is contained in:
Fabien Potencier 2010-11-23 15:11:28 +01:00
parent 450a6b39a2
commit b63de46374
6 changed files with 34 additions and 34 deletions

View File

@ -131,7 +131,7 @@ class RouteCompiler implements RouteCompilerInterface
if (false !== $this->tokenizeBufferBefore($buffer, $tokens, $afterASeparator, $currentSeparator)) {
// a custom token
$this->customToken = true;
} else if ($afterASeparator && preg_match('#^\:([\w\d_]+)#', $buffer, $match)) {
} else if ($afterASeparator && preg_match('#^\{([\w\d_]+)\}#', $buffer, $match)) {
// a variable
$this->tokens[] = array('variable', $currentSeparator, $match[0], $match[1]);

View File

@ -29,12 +29,12 @@ class ApacheMatcherDumperTest extends \PHPUnit_Framework_TestCase
$collection = new RouteCollection();
$collection->add('foo', new Route(
'/foo/:bar',
'/foo/{bar}',
array('def' => 'test'),
array('bar' => 'baz|symfony')
));
$collection->add('bar', new Route(
'/bar/:foo',
'/bar/{foo}',
array(),
array('_method' => 'GET|head')
));

View File

@ -29,12 +29,12 @@ class PhpMatcherDumperTest extends \PHPUnit_Framework_TestCase
$collection = new RouteCollection();
$collection->add('foo', new Route(
'/foo/:bar',
'/foo/{bar}',
array('def' => 'test'),
array('bar' => 'baz|symfony')
));
$collection->add('bar', new Route(
'/bar/:foo',
'/bar/{foo}',
array(),
array('_method' => 'GET|head')
));

View File

@ -34,14 +34,14 @@ class UrlMatcherTest extends \PHPUnit_Framework_TestCase
{
// test the patterns are matched are parameters are returned
$collection = new RouteCollection();
$collection->add('foo', new Route('/foo/:bar'));
$collection->add('foo', new Route('/foo/{bar}'));
$matcher = new UrlMatcher($collection, array(), array());
$this->assertEquals(false, $matcher->match('/no-match'));
$this->assertEquals(array('_route' => 'foo', 'bar' => 'baz'), $matcher->match('/foo/baz'));
// test that defaults are merged
$collection = new RouteCollection();
$collection->add('foo', new Route('/foo/:bar', array('def' => 'test')));
$collection->add('foo', new Route('/foo/{bar}', array('def' => 'test')));
$matcher = new UrlMatcher($collection, array(), array());
$this->assertEquals(array('_route' => 'foo', 'bar' => 'baz', 'def' => 'test'), $matcher->match('/foo/baz'));

View File

@ -44,44 +44,44 @@ class RouteCompilerTest extends \PHPUnit_Framework_TestCase
array(
'Route with a variable',
array('/foo/:bar'),
'/foo', '#^/foo/(?P<bar>[^/\.]+?)$#x', array('bar' => ':bar'), array(
array('variable', '/', ':bar', 'bar'),
array('/foo/{bar}'),
'/foo', '#^/foo/(?P<bar>[^/\.]+?)$#x', array('bar' => '{bar}'), array(
array('variable', '/', '{bar}', 'bar'),
array('text', '/', 'foo', null),
)),
array(
'Route with a variable that has a default value',
array('/foo/:bar', array('bar' => 'bar')),
'/foo', '#^/foo(?:/(?P<bar>[^/\.]+?))?$#x', array('bar' => ':bar'), array(
array('variable', '/', ':bar', 'bar'),
array('/foo/{bar}', array('bar' => 'bar')),
'/foo', '#^/foo(?:/(?P<bar>[^/\.]+?))?$#x', array('bar' => '{bar}'), array(
array('variable', '/', '{bar}', 'bar'),
array('text', '/', 'foo', null),
)),
array(
'Route with several variables',
array('/foo/:bar/:foobar'),
'/foo', '#^/foo/(?P<bar>[^/\.]+?)/(?P<foobar>[^/\.]+?)$#x', array('bar' => ':bar', 'foobar' => ':foobar'), array(
array('variable', '/', ':foobar', 'foobar'),
array('variable', '/', ':bar', 'bar'),
array('/foo/{bar}/{foobar}'),
'/foo', '#^/foo/(?P<bar>[^/\.]+?)/(?P<foobar>[^/\.]+?)$#x', array('bar' => '{bar}', 'foobar' => '{foobar}'), array(
array('variable', '/', '{foobar}', 'foobar'),
array('variable', '/', '{bar}', 'bar'),
array('text', '/', 'foo', null),
)),
array(
'Route with several variables that have default values',
array('/foo/:bar/:foobar', array('bar' => 'bar', 'foobar' => 'foobar')),
'/foo', '#^/foo(?:/(?P<bar>[^/\.]+?) (?:/(?P<foobar>[^/\.]+?) )?)?$#x', array('bar' => ':bar', 'foobar' => ':foobar'), array(
array('variable', '/', ':foobar', 'foobar'),
array('variable', '/', ':bar', 'bar'),
array('/foo/{bar}/{foobar}', array('bar' => 'bar', 'foobar' => 'foobar')),
'/foo', '#^/foo(?:/(?P<bar>[^/\.]+?) (?:/(?P<foobar>[^/\.]+?) )?)?$#x', array('bar' => '{bar}', 'foobar' => '{foobar}'), array(
array('variable', '/', '{foobar}', 'foobar'),
array('variable', '/', '{bar}', 'bar'),
array('text', '/', 'foo', null),
)),
array(
'Route with several variables but some of them have no default values',
array('/foo/:bar/:foobar', array('bar' => 'bar')),
'/foo', '#^/foo/(?P<bar>[^/\.]+?)/(?P<foobar>[^/\.]+?)$#x', array('bar' => ':bar', 'foobar' => ':foobar'), array(
array('variable', '/', ':foobar', 'foobar'),
array('variable', '/', ':bar', 'bar'),
array('/foo/{bar}/{foobar}', array('bar' => 'bar')),
'/foo', '#^/foo/(?P<bar>[^/\.]+?)/(?P<foobar>[^/\.]+?)$#x', array('bar' => '{bar}', 'foobar' => '{foobar}'), array(
array('variable', '/', '{foobar}', 'foobar'),
array('variable', '/', '{bar}', 'bar'),
array('text', '/', 'foo', null),
)),

View File

@ -17,8 +17,8 @@ class RouteTest extends \PHPUnit_Framework_TestCase
{
public function testConstructor()
{
$route = new Route('/:foo', array('foo' => 'bar'), array('foo' => '\d+'), array('foo' => 'bar'));
$this->assertEquals('/:foo', $route->getPattern(), '__construct() takes a pattern as its first argument');
$route = new Route('/{foo}', array('foo' => 'bar'), array('foo' => '\d+'), array('foo' => 'bar'));
$this->assertEquals('/{foo}', $route->getPattern(), '__construct() takes a pattern as its first argument');
$this->assertEquals(array('foo' => 'bar'), $route->getDefaults(), '__construct() takes defaults as its second argument');
$this->assertEquals(array('foo' => '\d+'), $route->getRequirements(), '__construct() takes requirements as its third argument');
$this->assertEquals('bar', $route->getOption('foo'), '__construct() takes options as its fourth argument');
@ -26,9 +26,9 @@ class RouteTest extends \PHPUnit_Framework_TestCase
public function testPattern()
{
$route = new Route('/:foo');
$route->setPattern('/:bar');
$this->assertEquals('/:bar', $route->getPattern(), '->setPattern() sets the pattern');
$route = new Route('/{foo}');
$route->setPattern('/{bar}');
$this->assertEquals('/{bar}', $route->getPattern(), '->setPattern() sets the pattern');
$route->setPattern('');
$this->assertEquals('/', $route->getPattern(), '->setPattern() adds a / at the beginning of the pattern if needed');
$route->setPattern('bar');
@ -38,7 +38,7 @@ class RouteTest extends \PHPUnit_Framework_TestCase
public function testOptions()
{
$route = new Route('/:foo');
$route = new Route('/{foo}');
$route->setOptions(array('foo' => 'bar'));
$this->assertEquals(array_merge(array(
'segment_separators' => array('/', '.'),
@ -56,7 +56,7 @@ class RouteTest extends \PHPUnit_Framework_TestCase
*/
public function testDefaults()
{
$route = new Route('/:foo');
$route = new Route('/{foo}');
$route->setDefaults(array('foo' => 'bar'));
$this->assertEquals(array('foo' => 'bar'), $route->getDefaults(), '->setDefaults() sets the defaults');
$this->assertEquals($route, $route->setDefaults(array()), '->setDefaults() implements a fluent interface');
@ -71,7 +71,7 @@ class RouteTest extends \PHPUnit_Framework_TestCase
public function testRequirements()
{
$route = new Route('/:foo');
$route = new Route('/{foo}');
$route->setRequirements(array('foo' => '\d+'));
$this->assertEquals(array('foo' => '\d+'), $route->getRequirements(), '->setRequirements() sets the requirements');
$this->assertEquals('\d+', $route->getRequirement('foo'), '->getRequirement() returns a requirement');
@ -87,7 +87,7 @@ class RouteTest extends \PHPUnit_Framework_TestCase
public function testCompile()
{
$route = new Route('/:foo');
$route = new Route('/{foo}');
$this->assertEquals('Symfony\\Component\\Routing\\CompiledRoute', get_class($compiled = $route->compile()), '->compile() returns a compiled route');
$this->assertEquals($compiled, $route->compile(), '->compile() only compiled the route once');
}