From ee94d5662c9a48a17231145ac92ab8c57ca3bd01 Mon Sep 17 00:00:00 2001 From: the_undefined Date: Sat, 19 Apr 2008 20:57:23 +0000 Subject: [PATCH] Added persist feature to routing, allows namedArgs to be auto-populated for reverse routing based on current request git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@6704 3807eeeb-6ff5-0310-8944-8be069107fe0 --- cake/libs/router.php | 5 ++++ cake/tests/cases/libs/router.test.php | 36 +++++++++++++++++++++++++++ 2 files changed, 41 insertions(+) diff --git a/cake/libs/router.php b/cake/libs/router.php index fb6d3ce1b..c8313776c 100644 --- a/cake/libs/router.php +++ b/cake/libs/router.php @@ -755,11 +755,16 @@ class Router extends Object { $match = false; foreach ($_this->routes as $route) { + $originalUrl = $url; + if (isset($route[4]['persist'], $_this->__params[0])) { + $url = am(array_intersect_key($params, Set::combine($route[4]['persist'], '/')), $url); + } if ($match = $_this->mapRouteElements($route, $url)) { $output = trim($match, '/'); $url = array(); break; } + $url = $originalUrl; } $named = $args = array(); $skip = array('bare', 'action', 'controller', 'plugin', 'ext', '?', '#', 'prefix', $admin); diff --git a/cake/tests/cases/libs/router.test.php b/cake/tests/cases/libs/router.test.php index 26a0d5100..786ac178f 100644 --- a/cake/tests/cases/libs/router.test.php +++ b/cake/tests/cases/libs/router.test.php @@ -671,6 +671,42 @@ class RouterTest extends UnitTestCase { $result = $this->router->parse('/posts/view/foo:bar/routing:fun/answer:42'); $expected = array('pass' => array('routing:fun'), 'named' => array('foo' => 'bar', 'answer' => '42'), 'plugin' => null, 'controller' => 'posts', 'action' => 'view'); $this->assertEqual($result, $expected); + + $this->router->reload(); + $this->router->connect('/posts/view/*', array('controller' => 'posts', 'action' => 'view'), array('named' => array('foo', 'answer'))); + $result = $this->router->parse('/posts/view/foo:bar/routing:fun/answer:42'); + $expected = array('pass' => array('routing:fun'), 'named' => array('foo' => 'bar', 'answer' => '42'), 'plugin' => null, 'controller' => 'posts', 'action' => 'view'); + $this->assertEqual($result, $expected); + + $this->router->reload(); + $this->router->connect('/:lang/:color/posts/view/*', array('controller' => 'posts', 'action' => 'view'), array('persist' => array('lang', 'color'))); + $this->router->connect('/:lang/:color/posts/index', array('controller' => 'posts', 'action' => 'index'), array('persist' => array('lang'))); + $this->router->connect('/:lang/:color/posts/edit/*', array('controller' => 'posts', 'action' => 'index')); + $this->router->connect('/about', array('controller' => 'pages', 'action' => 'view', 'about')); + $this->router->parse('/en/red/posts/view/5'); + $this->router->setRequestInfo(array( + array('controller' => 'posts', 'action' => 'view', 'lang' => 'en', 'color' => 'red', 'form' => array(), 'url' => array(), 'plugin' => null), + array('base' => '/', 'here' => '/en/red/posts/view/5', 'webroot' => '/', 'passedArgs' => array(), 'argSeparator' => ':', 'namedArgs' => array()) + )); + $expected = '/en/red/posts/view/6'; + $result = $this->router->url(array('controller' => 'posts', 'action' => 'view', 6)); + $this->assertEqual($result, $expected); + + $expected = '/en/blue/posts/index'; + $result = $this->router->url(array('controller' => 'posts', 'action' => 'index', 'color' => 'blue')); + $this->assertEqual($result, $expected); + + $expected = '/posts'; + $result = $this->router->url(array('controller' => 'posts', 'action' => 'index')); + $this->assertEqual($result, $expected); + + $expected = '/posts/edit/7'; + $result = $this->router->url(array('controller' => 'posts', 'action' => 'edit', 7)); + $this->assertEqual($result, $expected); + + $expected = '/about'; + $result = $this->router->url(array('controller' => 'pages', 'action' => 'view', 'about')); + $this->assertEqual($result, $expected); } function testUuidRoutes() {