Skip to content

Commit

Permalink
Adding more documentation to connectNamed().
Browse files Browse the repository at this point in the history
Changing argSeparator -> separator, as it is less typing and easier to remember.
  • Loading branch information
markstory committed Dec 20, 2010
1 parent 025ba23 commit d3fc29c
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
18 changes: 13 additions & 5 deletions cake/libs/router.php
Expand Up @@ -89,7 +89,7 @@ class Router {
* @access public
*/
public static $named = array(
'default' => array(':page', ':fields', ':order', ':limit', ':recursive', ':sort', ':direction', ':step'),
'default' => array('page', 'fields', 'order', 'limit', 'recursive', 'sort', 'direction', 'step'),
'greedy' => true,
'separator' => ':',
'rules' => false,
Expand Down Expand Up @@ -316,7 +316,7 @@ public static function redirect($route, $url, $options = array()) {
*
* {{{ Router::connectNamed(array('page' => '[\d]+'), array('default' => false, 'greedy' => false)); }}}
*
* Parse only the page parameter no mater what.
* Parse only the page parameter no matter what.
*
* {{{ Router::connectNamed(array('page'), array('default' => false, 'greedy' => false)); }}}
*
Expand All @@ -338,15 +338,23 @@ public static function redirect($route, $url, $options = array()) {
* );
* }}}
*
* ### Options
*
* - `greedy` Setting this to true will make Router parse all named params. Setting it to false will
* parse only the connected named params.
* - `default` Set this to true to merge in the default set of named parameters.
* - `reset` Set to true to clear existing rules and start fresh.
* - `separator` Change the string used to separate the key & value in a named parameter. Defaults to `:`
*
* @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
* @return array
*/
public static function connectNamed($named, $options = array()) {
if (isset($options['argSeparator'])) {
self::$named['separator'] = $options['argSeparator'];
unset($options['argSeparator']);
if (isset($options['separator'])) {
self::$named['separator'] = $options['separator'];
unset($options['separator']);
}

if ($named === true || $named === false) {
Expand Down
4 changes: 2 additions & 2 deletions cake/tests/cases/libs/router.test.php
Expand Up @@ -1335,14 +1335,14 @@ function testConnectNamed() {

Router::reload();
Router::connect('/foo/*', array('controller' => 'bar', 'action' => 'fubar'));
Router::connectNamed(array(), array('argSeparator' => '='));
Router::connectNamed(array(), array('separator' => '='));
$result = Router::parse('/foo/param1=value1/param2=value2');
$expected = array('pass' => array(), 'named' => array('param1' => 'value1', 'param2' => 'value2'), 'controller' => 'bar', 'action' => 'fubar', 'plugin' => null);
$this->assertEqual($result, $expected);

Router::reload();
Router::connect('/controller/action/*', array('controller' => 'controller', 'action' => 'action'), array('named' => array('param1' => 'value[\d]')));
Router::connectNamed(array(), array('greedy' => false, 'argSeparator' => '='));
Router::connectNamed(array(), array('greedy' => false, 'separator' => '='));
$result = Router::parse('/controller/action/param1=value1/param2=value2');
$expected = array('pass' => array('param2=value2'), 'named' => array('param1' => 'value1'), 'controller' => 'controller', 'action' => 'action', 'plugin' => null);
$this->assertEqual($result, $expected);
Expand Down

0 comments on commit d3fc29c

Please sign in to comment.