adding named to params so the proper args get passed to the action, updated tests

git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@5932 3807eeeb-6ff5-0310-8944-8be069107fe0
This commit is contained in:
gwoo 2007-10-31 03:53:41 +00:00
parent 9b578c5bb1
commit b1b8fcddd2
4 changed files with 56 additions and 50 deletions

View file

@ -186,7 +186,7 @@ class Dispatcher extends Object {
$controller->params =& $this->params;
$controller->action =& $this->params['action'];
$controller->webservices =& $this->params['webservices'];
$controller->passedArgs =& $this->params['pass'];
$controller->passedArgs = array_merge($this->params['pass'], $this->params['named']);
if (!empty($this->params['data'])) {
$controller->data =& $this->params['data'];
@ -273,6 +273,7 @@ class Dispatcher extends Object {
)
));
} else {
pr($params);
$output = call_user_func_array(array(&$controller, $params['action']), empty($params['pass'])? array(): $params['pass']);
}
@ -489,9 +490,11 @@ class Dispatcher extends Object {
$params = $this->_restructureParams($params);
}
if (!$ctrlClass = $this->__loadController($params)) {
extract(Router::getArgs($params['action']));
$params = am($params, array('controller'=> $params['plugin'],
'action'=> $params['controller'],
'pass' => am(Router::getArgs($params['action']), $params['pass'])
'pass' => am($pass, $params['pass']),
'named' => am($named, $params['named'])
)
);
if (!$ctrlClass = $this->__loadController($params)) {
@ -657,5 +660,4 @@ class Dispatcher extends Object {
}
}
}
?>

View file

@ -334,7 +334,7 @@ class Router extends Object {
function parse($url) {
$_this =& Router::getInstance();
$_this->__connectDefaultRoutes();
$out = array('pass' => array());
$out = array('pass' => array(), 'named'=>array());
$r = $ext = null;
if (ini_get('magic_quotes_gpc') == 1) {
@ -370,7 +370,6 @@ class Router extends Object {
}
}
}
foreach (Set::filter($r, true) as $key => $found) {
// if $found is a named url element (i.e. ':action')
if (isset($names[$key])) {
@ -378,7 +377,9 @@ class Router extends Object {
} elseif (isset($names[$key]) && empty($names[$key]) && empty($out[$names[$key]])) {
break; //leave the default values;
} else {
$out['pass'] = am($out['pass'], $_this->getArgs($found));
extract($_this->getArgs($found));
$out['pass'] = am($out['pass'], $pass);
$out['named'] = $named;
}
}
break;
@ -757,7 +758,6 @@ class Router extends Object {
if (((strpos($url, '://')) || (strpos($url, 'javascript:') === 0) || (strpos($url, 'mailto:') === 0)) || (substr($url, 0, 1) == '#')) {
return $url;
}
if (empty($url)) {
return $path['here'];
} elseif (substr($url, 0, 1) == '/') {
@ -767,7 +767,7 @@ class Router extends Object {
if ($admin && isset($params[$admin])) {
$output .= $admin . '/';
}
if (!empty($params['plugin'])) {
if (!empty($params['plugin']) && $params['plugin'] !== $params['controller']) {
$output .= Inflector::underscore($params['plugin']) . '/';
}
$output .= Inflector::underscore($params['controller']) . '/' . $url;
@ -780,6 +780,7 @@ class Router extends Object {
if (!empty($extension) && substr($output, -1) == '/') {
$output = substr($output, 0, -1);
}
return $output . $extension . $_this->queryString($q) . $frag;
}
/**
@ -1110,22 +1111,22 @@ class Router extends Object {
* @param array $params
* @static
*/
function getArgs($pass) {
function getArgs($args) {
$_this =& Router::getInstance();
$args = array();
$pass = array_map(
$pass = $named = array();
$args = array_map(
array(&$_this, 'stripEscape'),
Set::filter(explode('/', $pass), true)
Set::filter(explode('/', $args), true)
);
foreach ($pass as $param) {
foreach ($args as $param) {
if (strpos($param, $_this->__argSeparator)) {
$param = explode($_this->__argSeparator, $param);
$args[$param[0]] = $param[1];
$named[$param[0]] = $param[1];
} else {
$args[] = $param;
$pass[] = $param;
}
}
return $args;
return compact('pass', 'named');
}
}
?>

View file

@ -517,7 +517,8 @@ class DispatcherTest extends UnitTestCase {
$result = $dispatcher->parseParams($url);
$expected = array('pass' => array('home', 'param'=> 'value', 'param2'=> 'value2'),
$expected = array('pass' => array('home'),
'named' => array('param'=> 'value', 'param2'=> 'value2'),
'plugin'=> 'my_plugin', 'controller'=> 'some_pages', 'action'=> 'display',
'form'=> null, //array('testdata'=> 'My Posted Data'),
'url'=> array('url'=> 'my_plugin/some_pages/home/param:value/param2:value2'),
@ -583,6 +584,7 @@ class DispatcherTest extends UnitTestCase {
Router::reload();
$dispatcher =& new TestDispatcher();
$dispatcher->base = false;
$url = 'my_plugin/add/param:value/param2:value2';
restore_error_handler();
@ -599,13 +601,14 @@ class DispatcherTest extends UnitTestCase {
$this->assertIdentical($controller->action, $expected);
$expected = array('param'=>'value', 'param2'=>'value2');
$this->assertEqual($controller->params['pass'], $expected);
$this->assertEqual($controller->params['named'], $expected);
Router::reload();
Router::connect('/admin/:controller/:action/*', array('controller' => 'pages', 'action' => 'index', 'admin' => true, 'prefix' => 'admin'));
$dispatcher =& new TestDispatcher();
$dispatcher->base = false;
$url = 'admin/articles_test';
restore_error_handler();
@ -620,7 +623,7 @@ class DispatcherTest extends UnitTestCase {
$expected = 'admin_index';
$this->assertIdentical($controller->action, $expected);
$expected = array('pass'=> array(), 'controller' => 'articles_test', 'plugin' => 'articles_test', 'action' => 'admin_index',
$expected = array('pass'=> array(), 'named' => array(), 'controller' => 'articles_test', 'plugin' => 'articles_test', 'action' => 'admin_index',
'prefix' => 'admin', 'admin' => true, 'form' => array(), 'url' => array('url' => 'admin/articles_test'),
'bare' => 0, 'webservices' => null, 'return' => 1
);

View file

@ -97,23 +97,23 @@ class RouterTest extends UnitTestCase {
$_SERVER['REQUEST_METHOD'] = 'GET';
$result = $this->router->parse('/posts');
$this->assertEqual($result, array('pass' => array(), 'plugin' => '', 'controller' => 'posts', 'action' => 'index', '[method]' => 'GET'));
$this->assertEqual($result, array('pass' => array(), 'named' => array(), 'plugin' => '', 'controller' => 'posts', 'action' => 'index', '[method]' => 'GET'));
$_SERVER['REQUEST_METHOD'] = 'GET';
$result = $this->router->parse('/posts/13');
$this->assertEqual($result, array('pass' => array(), 'plugin' => '', 'controller' => 'posts', 'action' => 'view', 'id' => '13', '[method]' => 'GET'));
$this->assertEqual($result, array('pass' => array(), 'named' => array(), 'plugin' => '', 'controller' => 'posts', 'action' => 'view', 'id' => '13', '[method]' => 'GET'));
$_SERVER['REQUEST_METHOD'] = 'POST';
$result = $this->router->parse('/posts');
$this->assertEqual($result, array('pass' => array(), 'plugin' => '', 'controller' => 'posts', 'action' => 'add', '[method]' => 'POST'));
$this->assertEqual($result, array('pass' => array(), 'named' => array(), 'plugin' => '', 'controller' => 'posts', 'action' => 'add', '[method]' => 'POST'));
$_SERVER['REQUEST_METHOD'] = 'PUT';
$result = $this->router->parse('/posts/13');
$this->assertEqual($result, array('pass' => array(), 'plugin' => '', 'controller' => 'posts', 'action' => 'edit', 'id' => '13', '[method]' => 'PUT'));
$this->assertEqual($result, array('pass' => array(), 'named' => array(), 'plugin' => '', 'controller' => 'posts', 'action' => 'edit', 'id' => '13', '[method]' => 'PUT'));
$_SERVER['REQUEST_METHOD'] = 'DELETE';
$result = $this->router->parse('/posts/13');
$this->assertEqual($result, array('pass' => array(), 'plugin' => '', 'controller' => 'posts', 'action' => 'delete', 'id' => '13', '[method]' => 'DELETE'));
$this->assertEqual($result, array('pass' => array(), 'named' => array(), 'plugin' => '', 'controller' => 'posts', 'action' => 'delete', 'id' => '13', '[method]' => 'DELETE'));
}
function testUrlGeneration() {
@ -190,7 +190,7 @@ class RouterTest extends UnitTestCase {
),
array(
'base' => '/magazine', 'here' => '/magazine/admin/subscriptions/index/page:2',
'webroot' => '/magazine/', 'passedArgs' => array('page' => 2), 'namedArgs' => array('page' => 2),
'webroot' => '/magazine/', 'passedArgs' => array('page' => 2),
'webservices' => null
)
));
@ -514,36 +514,36 @@ class RouterTest extends UnitTestCase {
$this->router->connect('/posts/:value/:somevalue/:othervalue/*', array('controller' => 'posts', 'action' => 'view'), array('value','somevalue', 'othervalue'));
$result = $this->router->parse('/posts/2007/08/01/title-of-post-here');
$expected = array('value' => '2007', 'somevalue' => '08', 'othervalue' => '01', 'controller' => 'posts', 'action' => 'view', 'plugin' =>'', 'pass' => array('0' => 'title-of-post-here'));
$expected = array('value' => '2007', 'somevalue' => '08', 'othervalue' => '01', 'controller' => 'posts', 'action' => 'view', 'plugin' =>'', 'pass' => array('0' => 'title-of-post-here'), 'named' => array());
$this->assertEqual($result, $expected);
$this->router->routes = array();
$this->router->connect('/posts/:year/:month/:day/*', array('controller' => 'posts', 'action' => 'view'), array('year' => $Year, 'month' => $Month, 'day' => $Day));
$result = $this->router->parse('/posts/2007/08/01/title-of-post-here');
$expected = array('year' => '2007', 'month' => '08', 'day' => '01', 'controller' => 'posts', 'action' => 'view', 'plugin' =>'', 'pass' => array('0' => 'title-of-post-here'));
$expected = array('year' => '2007', 'month' => '08', 'day' => '01', 'controller' => 'posts', 'action' => 'view', 'plugin' =>'', 'pass' => array('0' => 'title-of-post-here'), 'named' => array());
$this->assertEqual($result, $expected);
$this->router->routes = array();
$this->router->connect('/posts/:day/:year/:month/*', array('controller' => 'posts', 'action' => 'view'), array('year' => $Year, 'month' => $Month, 'day' => $Day));
$result = $this->router->parse('/posts/01/2007/08/title-of-post-here');
$expected = array('day' => '01', 'year' => '2007', 'month' => '08', 'controller' => 'posts', 'action' => 'view', 'plugin' =>'', 'pass' => array('0' => 'title-of-post-here'));
$expected = array('day' => '01', 'year' => '2007', 'month' => '08', 'controller' => 'posts', 'action' => 'view', 'plugin' =>'', 'pass' => array('0' => 'title-of-post-here'), 'named' => array());
$this->assertEqual($result, $expected);
$this->router->routes = array();
$this->router->connect('/posts/:month/:day/:year//*', array('controller' => 'posts', 'action' => 'view'), array('year' => $Year, 'month' => $Month, 'day' => $Day));
$result = $this->router->parse('/posts/08/01/2007/title-of-post-here');
$expected = array('month' => '08', 'day' => '01', 'year' => '2007', 'controller' => 'posts', 'action' => 'view', 'plugin' =>'', 'pass' => array('0' => 'title-of-post-here'));
$expected = array('month' => '08', 'day' => '01', 'year' => '2007', 'controller' => 'posts', 'action' => 'view', 'plugin' =>'', 'pass' => array('0' => 'title-of-post-here'), 'named' => array());
$this->assertEqual($result, $expected);
$this->router->routes = array();
$this->router->connect('/posts/:year/:month/:day/*', array('controller' => 'posts', 'action' => 'view'));
$result = $this->router->parse('/posts/2007/08/01/title-of-post-here');
$expected = array('year' => '2007', 'month' => '08', 'day' => '01', 'controller' => 'posts', 'action' => 'view', 'plugin' =>'', 'pass' => array('0' => 'title-of-post-here'));
$expected = array('year' => '2007', 'month' => '08', 'day' => '01', 'controller' => 'posts', 'action' => 'view', 'plugin' =>'', 'pass' => array('0' => 'title-of-post-here'), 'named' => array());
$this->assertEqual($result, $expected);
$this->router->reload();
$result = $this->router->parse('/pages/display/home');
$expected = array('plugin' => null, 'pass' => array('home'), 'controller' => 'pages', 'action' => 'display');
$expected = array('plugin' => null, 'pass' => array('home'), 'controller' => 'pages', 'action' => 'display', 'named' => array());
$this->assertEqual($result, $expected);
$result = $this->router->parse('pages/display/home/');
@ -560,7 +560,7 @@ class RouterTest extends UnitTestCase {
$this->router->reload();
$this->router->connect('/:language/contact', array('language' => 'eng', 'plugin' => 'contact', 'controller' => 'contact', 'action' => 'index'), array('language' => '[a-z]{3}'));
$result = $this->router->parse('/eng/contact');
$expected = array('pass' => array(), 'language' => 'eng', 'plugin' => 'contact', 'controller' => 'contact', 'action' => 'index');
$expected = array('pass' => array(), 'named' => array(), 'language' => 'eng', 'plugin' => 'contact', 'controller' => 'contact', 'action' => 'index');
$this->assertEqual($result, $expected);
$this->router->reload();
@ -570,7 +570,7 @@ class RouterTest extends UnitTestCase {
);
$result = $this->router->parse('/forestillinger/10/2007/min-forestilling');
$expected = array('pass' => array('min-forestilling'), 'plugin' => 'shows', 'controller' => 'shows', 'action' => 'calendar', 'year' => 2007, 'month' => 10);
$expected = array('pass' => array('min-forestilling'), 'plugin' => 'shows', 'controller' => 'shows', 'action' => 'calendar', 'year' => 2007, 'month' => 10, 'named' => array());
$this->assertEqual($result, $expected);
}
@ -585,11 +585,11 @@ class RouterTest extends UnitTestCase {
);
$result = $this->router->parse('/some_extra/page/this_is_the_slug');
$expected = array('pass' => array(), 'plugin' => null, 'controller' => 'pages', 'action' => 'view', 'slug' => 'this_is_the_slug', 'extra' => 'some_extra');
$expected = array('pass' => array(), 'named' => array(), 'plugin' => null, 'controller' => 'pages', 'action' => 'view', 'slug' => 'this_is_the_slug', 'extra' => 'some_extra');
$this->assertEqual($result, $expected);
$result = $this->router->parse('/page/this_is_the_slug');
$expected = array( 'pass' => array(), 'plugin' => null, 'controller' => 'pages', 'action' => 'view', 'slug' => 'this_is_the_slug', 'extra' => null);
$expected = array( 'pass' => array(), 'named' => array(), 'plugin' => null, 'controller' => 'pages', 'action' => 'view', 'slug' => 'this_is_the_slug', 'extra' => null);
$this->assertEqual($result, $expected);
$this->router->reload();
@ -642,7 +642,7 @@ class RouterTest extends UnitTestCase {
$this->router->reload();
$result = $this->router->parse('admin/users/view/');
$expected = array('pass' => array(), 'controller' => 'users', 'action' => 'view', 'plugin' => null, 'prefix' => 'admin', 'admin' => true);
$expected = array('pass' => array(), 'named' => array(), 'controller' => 'users', 'action' => 'view', 'plugin' => null, 'prefix' => 'admin', 'admin' => true);
$this->assertEqual($result, $expected);
Configure::write('Routing.admin', 'beheer');
@ -654,7 +654,7 @@ class RouterTest extends UnitTestCase {
));
$result = $this->router->parse('beheer/users/view/');
$expected = array('pass' => array(), 'controller' => 'users', 'action' => 'view', 'plugin' => null, 'prefix' => 'beheer', 'beheer' => true);
$expected = array('pass' => array(), 'named' => array(), 'controller' => 'users', 'action' => 'view', 'plugin' => null, 'prefix' => 'beheer', 'beheer' => true);
$this->assertEqual($result, $expected);
@ -677,11 +677,11 @@ class RouterTest extends UnitTestCase {
$this->router->parseExtensions();
$result = $this->router->parse('/posts.rss');
$expected = array('plugin' => null, 'controller' => 'posts', 'action' => 'index', 'url' => array('ext' => 'rss'), 'pass'=> array());
$expected = array('plugin' => null, 'controller' => 'posts', 'action' => 'index', 'url' => array('ext' => 'rss'), 'pass'=> array(), 'named' => array());
$this->assertEqual($result, $expected);
$result = $this->router->parse('/posts/view/1.rss');
$expected = array('plugin' => null, 'controller' => 'posts', 'action' => 'view', 'pass' => array('1'), 'url' => array('ext' => 'rss'));
$expected = array('plugin' => null, 'controller' => 'posts', 'action' => 'view', 'pass' => array('1'), 'named' => array(), 'url' => array('ext' => 'rss'), 'named' => array());
$this->assertEqual($result, $expected);
$result = $this->router->parse('/posts/view/1.rss?query=test');
@ -695,11 +695,11 @@ class RouterTest extends UnitTestCase {
$this->router->parseExtensions('rss', 'xml');
$result = $this->router->parse('/posts.xml');
$expected = array('plugin' => null, 'controller' => 'posts', 'action' => 'index', 'url' => array('ext' => 'xml'), 'pass'=> array());
$expected = array('plugin' => null, 'controller' => 'posts', 'action' => 'index', 'url' => array('ext' => 'xml'), 'pass'=> array(), 'named' => array());
$this->assertEqual($result, $expected);
$result = $this->router->parse('/posts.atom?hello=goodbye');
$expected = array('plugin' => null, 'controller' => 'posts.atom', 'action' => 'index', 'pass' => array());
$expected = array('plugin' => null, 'controller' => 'posts.atom', 'action' => 'index', 'pass' => array(), 'named' => array());
$this->assertEqual($result, $expected);
$this->router->reload();
@ -807,7 +807,7 @@ class RouterTest extends UnitTestCase {
$this->router->connect('/', array('controller' => 'posts', 'action' => 'index'));
$this->router->connect('/view/:user/*', array('controller' => 'posts', 'action' => 'view'), array('user'));
$result = $this->router->parse('/view/gwoo/');
$expected = array('user' => 'gwoo', 'controller' => 'posts', 'action' => 'view', 'plugin' =>'', 'pass' => array());
$expected = array('user' => 'gwoo', 'controller' => 'posts', 'action' => 'view', 'plugin' =>'', 'pass' => array(), 'named' => array());
$this->assertEqual($result, $expected);
}
@ -817,11 +817,11 @@ class RouterTest extends UnitTestCase {
$this->router->connect('/pages/*', array('controller' => 'pages', 'action' => 'display'));
$result = $this->router->parse('/');
$expected = array('pass'=>array('home'), 'plugin' => null, 'controller' => 'pages', 'action' => 'display');
$expected = array('pass'=>array('home'), 'named' => array(), 'plugin' => null, 'controller' => 'pages', 'action' => 'display');
$this->assertEqual($result, $expected);
$result = $this->router->parse('/pages/home/');
$expected = array('pass' => array('home'), 'plugin' => null, 'controller' => 'pages', 'action' => 'display');
$expected = array('pass' => array('home'), 'named' => array(), 'plugin' => null, 'controller' => 'pages', 'action' => 'display', 'named' => array());
$this->assertEqual($result, $expected);
$this->router->reload();
@ -829,7 +829,7 @@ class RouterTest extends UnitTestCase {
$this->router->connect('/pages/*', array('controller' => 'pages', 'action' => 'display'));
$result = $this->router->parse('/pages/contact/');
$expected = array('pass'=>array('contact'), 'plugin'=> null, 'controller'=>'pages', 'action'=>'display');
$expected = array('pass'=>array('contact'), 'named' => array(), 'plugin'=> null, 'controller'=>'pages', 'action'=>'display', 'named' => array());
$this->assertEqual($result, $expected);
}
@ -846,7 +846,7 @@ class RouterTest extends UnitTestCase {
));
$result = $this->router->parse('/admin/posts/');
$expected = array('pass' => array(), 'prefix' => 'admin', 'plugin' => null, 'controller' => 'posts', 'action' => 'index', 'admin' => true);
$expected = array('pass' => array(), 'named' => array(), 'prefix' => 'admin', 'plugin' => null, 'controller' => 'posts', 'action' => 'index', 'admin' => true);
$this->assertEqual($result, $expected);
$result = $this->router->parse('/admin/posts');
@ -879,7 +879,7 @@ class RouterTest extends UnitTestCase {
$this->router->reload();
$this->router->setRequestInfo(array(
array('plugin' => null, 'controller' => 'images', 'action' => 'index', 'pass' => array(), 'prefix' => 'protected', 'admin' => false, 'form' => array(), 'url' => array ('url' => 'protected/images/index'), 'bare' => 0, 'webservices' => null),
array('plugin' => null, 'controller' => 'images', 'action' => 'index', 'pass' => array(), 'named' => array(), 'prefix' => 'protected', 'admin' => false, 'form' => array(), 'url' => array ('url' => 'protected/images/index'), 'bare' => 0, 'webservices' => null),
array('plugin' => null, 'controller' => null, 'action' => null, 'base' => '', 'here' => '/protected/images/index', 'webroot' => '/')
));
@ -904,15 +904,15 @@ class RouterTest extends UnitTestCase {
$this->router->connect('/:locale/:controller/:action/*', array(), array('locale' => 'dan|eng'));
$result = $this->router->parse('/test/test_action');
$expected = array('pass' => array(), 'controller' => 'test', 'action' => 'test_action', 'plugin' => null);
$expected = array('pass' => array(), 'named' => array(), 'controller' => 'test', 'action' => 'test_action', 'plugin' => null);
$this->assertEqual($result, $expected);
$result = $this->router->parse('/eng/test/test_action');
$expected = array('pass' => array(), 'locale' => 'eng', 'controller' => 'test', 'action' => 'test_action', 'plugin' => null);
$expected = array('pass' => array(), 'named' => array(), 'locale' => 'eng', 'controller' => 'test', 'action' => 'test_action', 'plugin' => null);
$this->assertEqual($result, $expected);
$result = $this->router->parse('/badness/test/test_action');
$expected = array('pass' => array('test_action'), 'controller' => 'badness', 'action' => 'test', 'plugin' => null);
$expected = array('pass' => array('test_action'), 'named' => array(), 'controller' => 'badness', 'action' => 'test', 'plugin' => null);
$this->assertEqual($result, $expected);
$this->router->reload();