Moving action modification when a prefix is detected so it affects all url arrays. Previously it was only applied to parameters in the current request. This fixes inconsistencies between request parameters and url parameters. Fixes #570

This commit is contained in:
Mark Story 2010-04-11 23:13:18 -04:00
parent 42bc252460
commit 404401b4de
2 changed files with 30 additions and 4 deletions

View file

@ -770,9 +770,6 @@ class Router {
} else { } else {
$params = end($self->__params); $params = end($self->__params);
} }
if (isset($params['prefix']) && strpos($params['action'], $params['prefix']) === 0) {
$params['action'] = substr($params['action'], strlen($params['prefix']) + 1);
}
} }
$path = array('base' => null); $path = array('base' => null);
@ -818,6 +815,9 @@ class Router {
} elseif (isset($url[$prefix]) && !$url[$prefix]) { } elseif (isset($url[$prefix]) && !$url[$prefix]) {
unset($url[$prefix]); unset($url[$prefix]);
} }
if (isset($url[$prefix]) && strpos($url['action'], $prefix) === 0) {
$url['action'] = substr($url['action'], strlen($prefix) + 1);
}
} }
$url += array('controller' => $params['controller'], 'plugin' => $params['plugin']); $url += array('controller' => $params['controller'], 'plugin' => $params['plugin']);

View file

@ -1741,7 +1741,7 @@ class RouterTest extends CakeTestCase {
Router::setRequestInfo(array( Router::setRequestInfo(array(
array('controller' => 'users', 'action' => 'login', 'company' => true, 'form' => array(), 'url' => array(), 'plugin' => null), array('controller' => 'users', 'action' => 'login', 'company' => true, 'form' => array(), 'url' => array(), 'plugin' => null),
array('base' => '/', 'here' => '/', 'webroot' => '/base/', 'passedArgs' => array(), 'argSeparator' => ':', 'namedArgs' => array()) array('base' => '/', 'here' => '/', 'webroot' => '/base/')
)); ));
$result = Router::url(array('controller' => 'users', 'action' => 'login', 'company' => false)); $result = Router::url(array('controller' => 'users', 'action' => 'login', 'company' => false));
@ -1749,6 +1749,32 @@ class RouterTest extends CakeTestCase {
$this->assertEqual($result, $expected); $this->assertEqual($result, $expected);
} }
/**
* test url generation with prefixes and custom routes
*
* @return void
*/
function testUrlWritingWithPrefixesAndCustomRoutes() {
Router::connect(
'/admin/login',
array('controller' => 'users', 'action' => 'login', 'prefix' => 'admin', 'admin' => true)
);
Router::setRequestInfo(array(
array('controller' => 'posts', 'action' => 'index', 'admin' => true, 'prefix' => 'admin',
'form' => array(), 'url' => array(), 'plugin' => null
),
array('base' => '/', 'here' => '/', 'webroot' => '/')
));
$result = Router::url(array('controller' => 'users', 'action' => 'login', 'admin' => true));
$this->assertEqual($result, '/admin/login');
$result = Router::url(array('controller' => 'users', 'action' => 'login'));
$this->assertEqual($result, '/admin/login');
$result = Router::url(array('controller' => 'users', 'action' => 'admin_login'));
$this->assertEqual($result, '/admin/login');
}
/** /**
* testPassedArgsOrder method * testPassedArgsOrder method
* *