diff --git a/cake/libs/router.php b/cake/libs/router.php index fbc3381aa..5440c1ad8 100644 --- a/cake/libs/router.php +++ b/cake/libs/router.php @@ -220,10 +220,28 @@ class Router extends Object { return $_this->routes; } /** - * Connects an array of named arguments (with optional scoping options) + *Specifies what named parameters CakePHP should be parsing. The most common setups are: + * + * Do not parse any named parameters: + * Router::connectNamed(false); + * + * Parse only default parameters used for CakePHP's pagination: + * Router::connectNamed(false, array('default' => true)); + * + * Parse only the page parameter if its value is a number: + * Router::connectNamed(array('page' => '[\d]+'), array('default' => false, 'greedy' => false)); + * + * Parse only the page parameter no mater what. + * Router::connectNamed(array('page'), array('default' => false, 'greedy' => false)); + * + * Parse only the page parameter if the current action is 'index'. + * Router::connectNamed(array('page' => array('action' => 'index')), array('default' => false, 'greedy' => false)); * - * @param array $named List of named arguments - * @param array $options Named argument handling options + * Parse only the page parameter if the current action is 'index' and the controller is 'pages'. + * Router::connectNamed(array('page' => array('action' => 'index', 'controller' => 'pages')), array('default' => false, 'greedy' => false)); + * + * @param array $named A list of named parameters. Key value pairs are accepted where values are either regex strings to match, or arrays as seen above. + * @param array $options Allows to control all settings: separator, greedy, reset, default * @access public * @static */ @@ -234,7 +252,7 @@ class Router extends Object { unset($options['argSeparator']); } if ($named === true || $named === false) { - $options = array('default' => $named, 'reset' => true, 'greedy' => $named); + $options = array_merge(array('default' => $named, 'reset' => true, 'greedy' => $named), $options); $named = array(); } $options = array_merge(array('default' => false, 'reset' => false, 'greedy' => true), $options); diff --git a/cake/tests/cases/libs/router.test.php b/cake/tests/cases/libs/router.test.php index 5af4fb076..17df593b2 100644 --- a/cake/tests/cases/libs/router.test.php +++ b/cake/tests/cases/libs/router.test.php @@ -905,6 +905,12 @@ class RouterTest extends UnitTestCase { ini_set('arg_separator.output', $restore); } + function testConnectNamed() { + $named = Router::connectNamed(false, array('default' => true)); + $this->assertFalse($named['greedy']); + $this->assertEqual(array_keys($named['rules']), $named['default']); + } + function testNamedArgsUrlGeneration() { $result = Router::url(array('controller' => 'posts', 'action' => 'index', 'published' => 1, 'deleted' => 1)); $expected = '/posts/index/published:1/deleted:1';