Permalink
Browse files

Fixing issues with optional parameters. Allows route params to be mad…

…e optional by setting a pattern definition.
  • Loading branch information...
1 parent fd98bc5 commit 49c6cd3f9f8d4e40efbe0a0330cc9ae281471af9 @markstory markstory committed Nov 27, 2009
Showing with 20 additions and 13 deletions.
  1. +12 −6 cake/libs/router.php
  2. +8 −7 cake/tests/cases/libs/router.test.php
View
@@ -1286,22 +1286,28 @@ function _writeRoute($route, $default, $params) {
if ($name !== 'plugin' && array_key_exists($name, $default)) {
$option = '?';
}
- $replacements[] = '(?:(' . $params[$name] . ')' . $option . ')' . $option;
+ $slashParam = '/\\' . $namedElements[0][$i];
+ if (strpos($parsed, $slashParam) !== false) {
+ $replacements[] = '(?:/(' . $params[$name] . ')' . $option . ')' . $option;
+ $search[] = $slashParam;
+ } else {
+ $search[] = '\\' . $namedElements[0][$i];
+ $replacements[] = '(?:(' . $params[$name] . ')' . $option . ')' . $option;
+ }
} else {
$replacements[] = '(?:([^/]+))?';
+ $search[] = '\\' . $namedElements[0][$i];
}
- $search[] = '\\' . $namedElements[0][$i];
$names[] = $name;
}
-
- $parsed = str_replace($search, $replacements, $parsed);
if (preg_match('#\/\*#', $route, $m)) {
$parsed = preg_replace('#/\\\\\*#', '(?:/(.*))?', $parsed);
}
-
+ $parsed = str_replace($search, $replacements, $parsed);
$this->_compiledRoute = '#^' . $parsed . '[/]*$#';
$this->keys = $names;
- /*
+
+ /*
$elements = explode('/', $route);
foreach ($elements as $element) {
@@ -2068,7 +2068,7 @@ function testRouteCompilingWithParamPatterns() {
extract(Router::getNamedExpressions());
$route = new RouterRoute(
- '/:controller/:action/:id',
+ '/:controller/:action/:id',
array('controller' => 'testing4', 'id' => null),
array('id' => $ID)
);
@@ -2097,7 +2097,7 @@ function testRouteCompilingWithParamPatterns() {
$this->assertPattern($result, '/posts/1' . $delim . 'name-of-article');
$this->assertPattern($result, '/posts/13244' . $delim . 'name-of_Article[]');
- $this->assertNoPattern($result, '/posts/11!!nameofarticle');
+ $this->assertNoPattern($result, '/posts/11!nameofarticle');
$this->assertNoPattern($result, '/posts/11');
$this->assertEqual($route->keys, array('id', 'title'));
@@ -2115,10 +2115,10 @@ function testRouteCompilingWithParamPatterns() {
$this->assertNoPattern($result, '/posts/:nameofarticle/2009');
$this->assertNoPattern($result, '/posts/:nameofarticle/01');
$this->assertEqual($route->keys, array('id', 'title', 'year'));
-
+
$route =& new RouterRoute(
'/posts/:url_title-(uuid::id)',
- array('controller' => 'posts', 'action' => 'view'),
+ array('controller' => 'posts', 'action' => 'view'),
array('pass' => array('id', 'url_title'), 'id' => $ID)
);
$result = $route->compile();
@@ -2148,7 +2148,7 @@ function testComplexRouteCompilingAndParsing() {
$this->assertPattern($result, '/pages/view/today');
$this->assertPattern($result, '/pages/view/today/tomorrow');
$this->assertNoPattern($result, '/pages/view/tomorrow/something:else');
-
+
$route =& new RouterRoute(
'/posts/:month/:day/:year/*',
array('controller' => 'posts', 'action' => 'view'), array('year' => $Year, 'month' => $Month, 'day' => $Day)
@@ -2164,6 +2164,9 @@ function testComplexRouteCompilingAndParsing() {
array("extra" => '[a-z1-9_]*', "slug" => '[a-z1-9_]+', "action" => 'view')
);
$result = $route->compile();
+
+ $this->assertPattern($result, '/some_extra/page/this_is_the_slug');
+ $this->assertPattern($result, '/page/this_is_the_slug');
$this->assertEqual($route->keys, array('extra', 'slug'));
$this->assertEqual($route->params, array('extra' => '[a-z1-9_]*', 'slug' => '[a-z1-9_]+', 'action' => 'view'));
$expected = array(
@@ -2173,8 +2176,6 @@ function testComplexRouteCompilingAndParsing() {
'plugin' => null
);
$this->assertEqual($route->defaults, $expected);
- $this->assertPattern($result, '/some_extra/page/this_is_the_slug');
- $this->assertNoPattern($result, '/page/this_is_the_slug');
}
/**

0 comments on commit 49c6cd3

Please sign in to comment.