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
This commit is contained in:
the_undefined 2008-04-19 20:57:23 +00:00
parent 296e8989ba
commit ee94d5662c
2 changed files with 41 additions and 0 deletions

View file

@ -755,11 +755,16 @@ class Router extends Object {
$match = false; $match = false;
foreach ($_this->routes as $route) { 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)) { if ($match = $_this->mapRouteElements($route, $url)) {
$output = trim($match, '/'); $output = trim($match, '/');
$url = array(); $url = array();
break; break;
} }
$url = $originalUrl;
} }
$named = $args = array(); $named = $args = array();
$skip = array('bare', 'action', 'controller', 'plugin', 'ext', '?', '#', 'prefix', $admin); $skip = array('bare', 'action', 'controller', 'plugin', 'ext', '?', '#', 'prefix', $admin);

View file

@ -671,6 +671,42 @@ class RouterTest extends UnitTestCase {
$result = $this->router->parse('/posts/view/foo:bar/routing:fun/answer:42'); $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'); $expected = array('pass' => array('routing:fun'), 'named' => array('foo' => 'bar', 'answer' => '42'), 'plugin' => null, 'controller' => 'posts', 'action' => 'view');
$this->assertEqual($result, $expected); $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() { function testUuidRoutes() {