Skip to content

Commit

Permalink
Fixing issues with optional parameters. Allows route params to be mad…
Browse files Browse the repository at this point in the history
…e optional by setting a pattern definition.
  • Loading branch information
markstory committed Nov 27, 2009
1 parent fd98bc5 commit 49c6cd3
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 13 deletions.
18 changes: 12 additions & 6 deletions cake/libs/router.php
Expand Up @@ -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) {
Expand Down
15 changes: 8 additions & 7 deletions cake/tests/cases/libs/router.test.php
Expand Up @@ -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)
);
Expand Down Expand Up @@ -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'));
Expand All @@ -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();
Expand Down Expand Up @@ -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)
Expand All @@ -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(
Expand All @@ -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');
}

/**
Expand Down

0 comments on commit 49c6cd3

Please sign in to comment.