Skip to content

Commit

Permalink
Allowing persistent route parameters to be overridden by setting to `…
Browse files Browse the repository at this point in the history
…null`.
  • Loading branch information
nateabele committed Apr 20, 2010
1 parent 807365a commit 65e9b52
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
8 changes: 6 additions & 2 deletions libraries/lithium/net/http/Router.php
Expand Up @@ -127,13 +127,17 @@ public static function match($options = array(), $context = null) {
unset($options[0]);
$options = $params + $options;
}
$defaults = array('action' => 'index');

if ($context && isset($context->persist)) {
foreach ($context->persist as $key) {
$defaults[$key] = $context->params[$key];
$options += array($key => $context->params[$key]);
if ($options[$key] === null) {
unset($options[$key]);
}
}
}

$defaults = array('action' => 'index');
$options += $defaults;
$base = isset($context) ? $context->env('base') : '';

Expand Down
25 changes: 25 additions & 0 deletions libraries/lithium/tests/cases/net/http/RouterTest.php
Expand Up @@ -387,6 +387,31 @@ public function testParameterPersistence() {
$this->assertEqual('/add/baz/dib', $path);
}

/**
* Tests that persistent parameters can be overridden with nulled-out values.
*
* @return void
*/
public function testOverridingPersistentParameters() {
Router::connect(
'/admin/{:controller}/{:action}',
array('admin' => true),
array('persist' => array('admin', 'controller'))
);
Router::connect('/{:controller}/{:action}');

$request = Router::process(new Request(array('url' => '/admin/posts/add', 'base' => '')));
$expected = array('controller' => 'posts', 'action' => 'add', 'admin' => true);
$this->assertEqual($expected, $request->params);
$this->assertEqual(array('admin', 'controller'), $request->persist);

$url = Router::match(array('action' => 'archive'), $request);
$this->assertEqual('/admin/posts/archive', $url);

$url = Router::match(array('action' => 'archive', 'admin' => null), $request);
$this->assertEqual('/posts/archive', $url);
}

/**
* Tests passing a closure handler to `Router::connect()` to bypass or augment default
* dispatching.
Expand Down

0 comments on commit 65e9b52

Please sign in to comment.