From 65e9b52edfc7d92abdd4664db2d67032b22ca51d Mon Sep 17 00:00:00 2001 From: Nate Abele Date: Mon, 19 Apr 2010 21:01:27 -0400 Subject: [PATCH] Allowing persistent route parameters to be overridden by setting to `null`. --- libraries/lithium/net/http/Router.php | 8 ++++-- .../tests/cases/net/http/RouterTest.php | 25 +++++++++++++++++++ 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/libraries/lithium/net/http/Router.php b/libraries/lithium/net/http/Router.php index aed6eea7d2..83d479c85e 100644 --- a/libraries/lithium/net/http/Router.php +++ b/libraries/lithium/net/http/Router.php @@ -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') : ''; diff --git a/libraries/lithium/tests/cases/net/http/RouterTest.php b/libraries/lithium/tests/cases/net/http/RouterTest.php index f313efcd65..20d5e89cc6 100644 --- a/libraries/lithium/tests/cases/net/http/RouterTest.php +++ b/libraries/lithium/tests/cases/net/http/RouterTest.php @@ -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.