Skip to content

Commit f433f6b

Browse files
committed
fixed route regex when the pattern is only made of optional segments
1 parent 2229461 commit f433f6b

File tree

3 files changed

+18
-2
lines changed

3 files changed

+18
-2
lines changed

src/Symfony/Component/Routing/RouteCompiler.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ private function computeRegexp(array $tokens, $index, $firstOptional)
114114
return preg_quote($token[1], self::REGEX_DELIMITER);
115115
} else {
116116
// Variable tokens
117-
if (0 === $index && 0 === $firstOptional && 1 == count($tokens)) {
117+
if (0 === $index && 0 === $firstOptional) {
118118
// When the only token is an optional variable token, the separator is required
119119
return sprintf('%s(?<%s>%s)?', preg_quote($token[1], self::REGEX_DELIMITER), $token[3], $token[2]);
120120
} else {
@@ -127,7 +127,7 @@ private function computeRegexp(array $tokens, $index, $firstOptional)
127127
$nbTokens = count($tokens);
128128
if ($nbTokens - 1 == $index) {
129129
// Close the optional subpatterns
130-
$regexp .= str_repeat(")?", $nbTokens - $firstOptional);
130+
$regexp .= str_repeat(")?", $nbTokens - $firstOptional - (0 === $firstOptional ? 1 : 0));
131131
}
132132
}
133133

src/Symfony/Component/Routing/Tests/Matcher/UrlMatcherTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,14 @@ public function testMatch()
118118
$matcher = new UrlMatcher($collection, new RequestContext(), array());
119119
$this->assertEquals(array('_route' => 'bar', 'bar' => 'foo'), $matcher->match('/foo'));
120120
$this->assertEquals(array('_route' => 'bar', 'bar' => 'bar'), $matcher->match('/'));
121+
122+
// route with only optional variables
123+
$collection = new RouteCollection();
124+
$collection->add('bar', new Route('/{foo}/{bar}', array('foo' => 'foo', 'bar' => 'bar'), array()));
125+
$matcher = new UrlMatcher($collection, new RequestContext(), array());
126+
$this->assertEquals(array('_route' => 'bar', 'foo' => 'foo', 'bar' => 'bar'), $matcher->match('/'));
127+
$this->assertEquals(array('_route' => 'bar', 'foo' => 'a', 'bar' => 'bar'), $matcher->match('/a'));
128+
$this->assertEquals(array('_route' => 'bar', 'foo' => 'a', 'bar' => 'b'), $matcher->match('/a/b'));
121129
}
122130

123131
public function testMatchWithPrefixes()

src/Symfony/Component/Routing/Tests/RouteCompilerTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,14 @@ public function provideCompileData()
9797
array('variable', '/', '(foo|bar)', 'bar'),
9898
)),
9999

100+
array(
101+
'Route with only optional variables',
102+
array('/{foo}/{bar}', array('foo' => 'foo', 'bar' => 'bar')),
103+
'', '#^/(?<foo>[^/]+)?(?:/(?<bar>[^/]+))?$#s', array('foo', 'bar'), array(
104+
array('variable', '/', '[^/]+', 'bar'),
105+
array('variable', '/', '[^/]+', 'foo'),
106+
)),
107+
100108
array(
101109
'Route with a variable in last position',
102110
array('/foo-{bar}'),

0 commit comments

Comments
 (0)