Skip to content

Commit

Permalink
[Routing] fixed deep nested route collections (closes #770)
Browse files Browse the repository at this point in the history
  • Loading branch information
fabpot committed May 4, 2011
1 parent 36bcfcc commit fe9ef5c
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/Symfony/Component/Routing/RouteCollection.php
Expand Up @@ -134,8 +134,12 @@ public function addPrefix($prefix)


$this->prefix = $prefix.$this->prefix; $this->prefix = $prefix.$this->prefix;


foreach ($this->all() as $route) { foreach ($this->routes as $name => $route) {
$route->setPattern($prefix.$route->getPattern()); if ($route instanceof RouteCollection) {
$route->addPrefix($prefix);
} else {
$route->setPattern($prefix.$route->getPattern());
}
} }
} }


Expand Down
Expand Up @@ -88,6 +88,24 @@ public function match($pathinfo)
return array ( 'def' => 'test', '_route' => 'foofoo',); return array ( 'def' => 'test', '_route' => 'foofoo',);
} }


if (0 === strpos($pathinfo, '/a')) {
if (0 === strpos($pathinfo, '/a/b')) {
// foo
if (0 === strpos($pathinfo, '/a/b') && preg_match('#^/a/b/(?P<foo>[^/]+?)$#x', $pathinfo, $matches)) {
$matches['_route'] = 'foo';
return $matches;
}

// bar
if (0 === strpos($pathinfo, '/a/b') && preg_match('#^/a/b/(?P<bar>[^/]+?)$#x', $pathinfo, $matches)) {
$matches['_route'] = 'bar';
return $matches;
}

}

}

throw 0 < count($allow) ? new MethodNotAllowedException(array_unique($allow)) : new NotFoundException(); throw 0 < count($allow) ? new MethodNotAllowedException(array_unique($allow)) : new NotFoundException();
} }
} }
Expand Up @@ -100,6 +100,24 @@ public function match($pathinfo)
return array ( 'def' => 'test', '_route' => 'foofoo',); return array ( 'def' => 'test', '_route' => 'foofoo',);
} }


if (0 === strpos($pathinfo, '/a')) {
if (0 === strpos($pathinfo, '/a/b')) {
// foo
if (0 === strpos($pathinfo, '/a/b') && preg_match('#^/a/b/(?P<foo>[^/]+?)$#x', $pathinfo, $matches)) {
$matches['_route'] = 'foo';
return $matches;
}

// bar
if (0 === strpos($pathinfo, '/a/b') && preg_match('#^/a/b/(?P<bar>[^/]+?)$#x', $pathinfo, $matches)) {
$matches['_route'] = 'bar';
return $matches;
}

}

}

// secure // secure
if ($pathinfo === '/secure') { if ($pathinfo === '/secure') {
if ($this->context->getScheme() !== 'https') { if ($this->context->getScheme() !== 'https') {
Expand Down
Expand Up @@ -68,6 +68,14 @@ public function testDump()
array('def' => 'test') array('def' => 'test')
)); ));


// prefixes
$collection1 = new RouteCollection();
$collection1->add('foo', new Route('/{foo}'));
$collection1->add('bar', new Route('/{bar}'));
$collection2 = new RouteCollection();
$collection2->addCollection($collection1, '/b');
$collection->addCollection($collection2, '/a');

$dumper = new PhpMatcherDumper($collection, new RequestContext()); $dumper = new PhpMatcherDumper($collection, new RequestContext());
$this->assertStringEqualsFile(__DIR__.'/../../Fixtures/dumper/url_matcher1.php', $dumper->dump(), '->dump() dumps basic routes to the correct PHP file.'); $this->assertStringEqualsFile(__DIR__.'/../../Fixtures/dumper/url_matcher1.php', $dumper->dump(), '->dump() dumps basic routes to the correct PHP file.');


Expand Down
15 changes: 15 additions & 0 deletions tests/Symfony/Tests/Component/Routing/Matcher/UrlMatcherTest.php
Expand Up @@ -111,6 +111,21 @@ public function testMatch()
$this->assertEquals(array('_route' => 'bar', 'bar' => 'bar'), $matcher->match('/')); $this->assertEquals(array('_route' => 'bar', 'bar' => 'bar'), $matcher->match('/'));
} }


public function testMatchWithPrefixes()
{
$collection1 = new RouteCollection();
$collection1->add('foo', new Route('/{foo}'));

$collection2 = new RouteCollection();
$collection2->addCollection($collection1, '/b');

$collection = new RouteCollection();
$collection->addCollection($collection2, '/a');

$matcher = new UrlMatcher($collection, new RequestContext(), array());
$this->assertEquals(array('_route' => 'foo', 'foo' => 'foo'), $matcher->match('/a/b/foo'));
}

public function testMatchRegression() public function testMatchRegression()
{ {
$coll = new RouteCollection(); $coll = new RouteCollection();
Expand Down

0 comments on commit fe9ef5c

Please sign in to comment.