fixes #5866 and fixes #6069, bugs in dispatcher with plugins and params

git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@8033 3807eeeb-6ff5-0310-8944-8be069107fe0
This commit is contained in:
gwoo 2009-02-16 00:18:59 +00:00
parent ce4fa20f3f
commit b35de27de1
3 changed files with 48 additions and 43 deletions

View file

@ -87,7 +87,6 @@ class Dispatcher extends Object {
if ($base !== false) { if ($base !== false) {
Configure::write('App.base', $base); Configure::write('App.base', $base);
} }
if ($url !== null) { if ($url !== null) {
return $this->dispatch($url); return $this->dispatch($url);
} }
@ -105,33 +104,26 @@ class Dispatcher extends Object {
* @access public * @access public
*/ */
function dispatch($url = null, $additionalParams = array()) { function dispatch($url = null, $additionalParams = array()) {
$parse = true;
if (is_array($url)) {
$url = $this->__extractParams($url, $additionalParams);
$parse = false;
}
if ($this->base === false) { if ($this->base === false) {
$this->base = $this->baseUrl(); $this->base = $this->baseUrl();
} }
if ($url !== null) { if (is_array($url)) {
$url = $this->__extractParams($url, $additionalParams);
} else {
if ($url) {
$_GET['url'] = $url; $_GET['url'] = $url;
} }
if ($parse) {
$url = $this->getUrl(); $url = $this->getUrl();
$this->params = array_merge($this->parseParams($url), $additionalParams);
} }
$this->here = $this->base . '/' . $url; $this->here = $this->base . '/' . $url;
if ($this->cached($url)) { if ($this->cached($url)) {
$this->_stop(); $this->_stop();
} }
if ($parse) {
$this->params = array_merge($this->parseParams($url), $additionalParams);
}
$controller =& $this->__getController(); $controller =& $this->__getController();
if (!is_object($controller)) { if (!is_object($controller)) {
@ -270,8 +262,7 @@ class Dispatcher extends Object {
function __extractParams($url, $additionalParams = array()) { function __extractParams($url, $additionalParams = array()) {
$defaults = array('pass' => array(), 'named' => array(), 'form' => array()); $defaults = array('pass' => array(), 'named' => array(), 'form' => array());
$this->params = array_merge($defaults, $url, $additionalParams); $this->params = array_merge($defaults, $url, $additionalParams);
//$url = Router::url($url); return Router::url($url);
//return $url;
} }
/** /**
* Returns array of GET and POST parameters. GET parameters are taken from given URL. * Returns array of GET and POST parameters. GET parameters are taken from given URL.
@ -300,19 +291,18 @@ class Dispatcher extends Object {
unset($params['form']['_method']); unset($params['form']['_method']);
} }
} }
extract(Router::getNamedExpressions()); $namedExpressions = Router::getNamedExpressions();
extract($namedExpressions);
include CONFIGS . 'routes.php'; include CONFIGS . 'routes.php';
$params = array_merge(Router::parse($fromUrl), $params); $params = array_merge(Router::parse($fromUrl), $params);
if (empty($params['action'])) { if (strlen($params['action']) === 0) {
$params['action'] = 'index'; $params['action'] = 'index';
} }
if (isset($params['form']['data'])) { if (isset($params['form']['data'])) {
$params['data'] = Router::stripEscape($params['form']['data']); $params['data'] = Router::stripEscape($params['form']['data']);
unset($params['form']['data']); unset($params['form']['data']);
} }
if (isset($_GET)) { if (isset($_GET)) {
if (ini_get('magic_quotes_gpc') === '1') { if (ini_get('magic_quotes_gpc') === '1') {
$url = stripslashes_deep($_GET); $url = stripslashes_deep($_GET);
@ -325,13 +315,11 @@ class Dispatcher extends Object {
$params['url'] = $url; $params['url'] = $url;
} }
} }
foreach ($_FILES as $name => $data) { foreach ($_FILES as $name => $data) {
if ($name != 'data') { if ($name != 'data') {
$params['form'][$name] = $data; $params['form'][$name] = $data;
} }
} }
if (isset($_FILES['data'])) { if (isset($_FILES['data'])) {
foreach ($_FILES['data'] as $key => $data) { foreach ($_FILES['data'] as $key => $data) {
foreach ($data as $model => $fields) { foreach ($data as $model => $fields) {
@ -363,12 +351,10 @@ class Dispatcher extends Object {
if (!$base) { if (!$base) {
$base = $this->base; $base = $this->base;
} }
if ($base !== false) { if ($base !== false) {
$this->webroot = $base . '/'; $this->webroot = $base . '/';
return $this->base = $base; return $this->base = $base;
} }
if (!$baseUrl) { if (!$baseUrl) {
$base = dirname(env('PHP_SELF')); $base = dirname(env('PHP_SELF'));
@ -418,10 +404,12 @@ class Dispatcher extends Object {
function _restructureParams($params, $reverse = false) { function _restructureParams($params, $reverse = false) {
if ($reverse === true) { if ($reverse === true) {
extract(Router::getArgs($params['action'])); extract(Router::getArgs($params['action']));
$params = array_merge($params, array('controller'=> $params['plugin'], $params = array_merge($params, array(
'controller'=> $params['plugin'],
'action'=> $params['controller'], 'action'=> $params['controller'],
'pass' => array_merge($pass, $params['pass']), 'pass' => array_merge($pass, $params['pass']),
'named' => array_merge($named, $params['named']))); 'named' => array_merge($named, $params['named'])
));
$this->plugin = $params['plugin']; $this->plugin = $params['plugin'];
} else { } else {
$params['plugin'] = $params['controller']; $params['plugin'] = $params['controller'];
@ -447,21 +435,25 @@ class Dispatcher extends Object {
$params = $this->params; $params = $this->params;
} }
$controller = false; $controller = false;
$ctrlClass = $this->__loadController($params);
if (!$ctrlClass = $this->__loadController($params)) { if (!$ctrlClass) {
if (!isset($params['plugin'])) { if (!isset($params['plugin'])) {
$params = $this->_restructureParams($params); $params = $this->_restructureParams($params);
} else { } else {
if (empty($this->params['pass']) && $params['action'] == 'index') {
$params['action'] = null;
}
$params = $this->_restructureParams($params, true); $params = $this->_restructureParams($params, true);
} }
$ctrlClass = $this->__loadController($params);
if (!$ctrlClass = $this->__loadController($params)) { if (!$ctrlClass) {
return $controller; return $controller;
} }
} else {
$params = $this->params;
} }
$name = $ctrlClass; $name = $ctrlClass;
$ctrlClass = $ctrlClass . 'Controller'; $ctrlClass = $ctrlClass . 'Controller';
if (class_exists($ctrlClass)) { if (class_exists($ctrlClass)) {
if (strtolower(get_parent_class($ctrlClass)) === strtolower($name . 'AppController') && empty($params['plugin'])) { if (strtolower(get_parent_class($ctrlClass)) === strtolower($name . 'AppController') && empty($params['plugin'])) {
$params = $this->_restructureParams($params); $params = $this->_restructureParams($params);
@ -481,7 +473,6 @@ class Dispatcher extends Object {
*/ */
function __loadController($params) { function __loadController($params) {
$pluginName = $pluginPath = $controller = null; $pluginName = $pluginPath = $controller = null;
if (!empty($params['plugin'])) { if (!empty($params['plugin'])) {
$this->plugin = $params['plugin']; $this->plugin = $params['plugin'];
$pluginName = Inflector::camelize($params['plugin']); $pluginName = Inflector::camelize($params['plugin']);
@ -489,11 +480,9 @@ class Dispatcher extends Object {
$this->params['controller'] = $this->plugin; $this->params['controller'] = $this->plugin;
$controller = $pluginName; $controller = $pluginName;
} }
if (!empty($params['controller'])) { if (!empty($params['controller'])) {
$controller = Inflector::camelize($params['controller']); $controller = Inflector::camelize($params['controller']);
} }
if ($pluginPath . $controller) { if ($pluginPath . $controller) {
if (App::import('Controller', $pluginPath . $controller)) { if (App::import('Controller', $pluginPath . $controller)) {
return $controller; return $controller;

View file

@ -1325,6 +1325,9 @@ class DispatcherTest extends CakeTestCase {
* @access public * @access public
* @return void * @return void
*/ */
function igetTests() {
return array('testAutomaticPluginDispatchWithShortAccess');
}
function testAutomaticPluginControllerDispatch() { function testAutomaticPluginControllerDispatch() {
$_POST = array(); $_POST = array();
$_SERVER['PHP_SELF'] = '/cake/repo/branches/1.2.x.x/index.php'; $_SERVER['PHP_SELF'] = '/cake/repo/branches/1.2.x.x/index.php';
@ -1431,26 +1434,39 @@ class DispatcherTest extends CakeTestCase {
Router::reload(); Router::reload();
Router::connect('/my_plugin/:controller/:action/*', array('plugin'=>'my_plugin')); Router::connect('/my_plugin/:controller/:action/*', array('plugin'=>'my_plugin'));
$Dispatcher =& new TestDispatcher();
$Dispatcher =& new TestDispatcher();
$Dispatcher->base = false; $Dispatcher->base = false;
$url = 'my_plugin/my_plugin/add'; $url = 'my_plugin/my_plugin/add';
$controller = $Dispatcher->dispatch($url, array('return' => 1)); $controller = $Dispatcher->dispatch($url, array('return' => 1));
$this->assertFalse(isset($controller->params['pass'][0])); $this->assertFalse(isset($controller->params['pass'][0]));
$Dispatcher =& new TestDispatcher();
$Dispatcher->base = false;
$url = 'my_plugin/my_plugin/add/0'; $url = 'my_plugin/my_plugin/add/0';
$controller = $Dispatcher->dispatch($url, array('return' => 1)); $controller = $Dispatcher->dispatch($url, array('return' => 1));
$this->assertTrue(isset($controller->params['pass'][0])); $this->assertTrue(isset($controller->params['pass'][0]));
$Dispatcher =& new TestDispatcher();
$Dispatcher->base = false;
$url = 'my_plugin/add'; $url = 'my_plugin/add';
$controller = $Dispatcher->dispatch($url, array('return' => 1)); $controller = $Dispatcher->dispatch($url, array('return' => 1));
$this->assertFalse(isset($controller->params['pass'][0])); $this->assertFalse(isset($controller->params['pass'][0]));
$Dispatcher =& new TestDispatcher();
$Dispatcher->base = false;
$url = 'my_plugin/add/0'; $url = 'my_plugin/add/0';
$controller = $Dispatcher->dispatch($url, array('return' => 1)); $controller = $Dispatcher->dispatch($url, array('return' => 1));
$this->assertIdentical('0',$controller->params['pass'][0]); $this->assertIdentical('0',$controller->params['pass'][0]);
$Dispatcher =& new TestDispatcher();
$Dispatcher->base = false;
$url = 'my_plugin/add/1'; $url = 'my_plugin/add/1';
$controller = $Dispatcher->dispatch($url, array('return' => 1)); $controller = $Dispatcher->dispatch($url, array('return' => 1));
$this->assertIdentical('1',$controller->params['pass'][0]); $this->assertIdentical('1',$controller->params['pass'][0]);

View file

@ -580,7 +580,7 @@ class ObjectTest extends CakeTestCase {
$expected = array(); $expected = array();
$this->assertEqual($expected, $result); $this->assertEqual($expected, $result);
$result = $this->object->requestAction(array('controller' => 'request_action', 'action' => 'post_pass', 'data' => $_POST['data'])); $result = $this->object->requestAction(array('controller' => 'request_action', 'action' => 'post_pass'), array('data' => $_POST['data']));
$expected = $_POST['data']; $expected = $_POST['data'];
$this->assertEqual($expected, $result); $this->assertEqual($expected, $result);