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

View file

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

View file

@ -580,7 +580,7 @@ class ObjectTest extends CakeTestCase {
$expected = array();
$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'];
$this->assertEqual($expected, $result);