Skip to content

Commit 404401b

Browse files
committed
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
1 parent 42bc252 commit 404401b

File tree

2 files changed

+30
-4
lines changed

2 files changed

+30
-4
lines changed

cake/libs/router.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -770,9 +770,6 @@ function url($url = null, $full = false) {
770770
} else {
771771
$params = end($self->__params);
772772
}
773-
if (isset($params['prefix']) && strpos($params['action'], $params['prefix']) === 0) {
774-
$params['action'] = substr($params['action'], strlen($params['prefix']) + 1);
775-
}
776773
}
777774
$path = array('base' => null);
778775

@@ -818,6 +815,9 @@ function url($url = null, $full = false) {
818815
} elseif (isset($url[$prefix]) && !$url[$prefix]) {
819816
unset($url[$prefix]);
820817
}
818+
if (isset($url[$prefix]) && strpos($url['action'], $prefix) === 0) {
819+
$url['action'] = substr($url['action'], strlen($prefix) + 1);
820+
}
821821
}
822822

823823
$url += array('controller' => $params['controller'], 'plugin' => $params['plugin']);

cake/tests/cases/libs/router.test.php

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1741,14 +1741,40 @@ function testUrlWritingWithPrefixes() {
17411741

17421742
Router::setRequestInfo(array(
17431743
array('controller' => 'users', 'action' => 'login', 'company' => true, 'form' => array(), 'url' => array(), 'plugin' => null),
1744-
array('base' => '/', 'here' => '/', 'webroot' => '/base/', 'passedArgs' => array(), 'argSeparator' => ':', 'namedArgs' => array())
1744+
array('base' => '/', 'here' => '/', 'webroot' => '/base/')
17451745
));
17461746

17471747
$result = Router::url(array('controller' => 'users', 'action' => 'login', 'company' => false));
17481748
$expected = '/login';
17491749
$this->assertEqual($result, $expected);
17501750
}
17511751

1752+
/**
1753+
* test url generation with prefixes and custom routes
1754+
*
1755+
* @return void
1756+
*/
1757+
function testUrlWritingWithPrefixesAndCustomRoutes() {
1758+
Router::connect(
1759+
'/admin/login',
1760+
array('controller' => 'users', 'action' => 'login', 'prefix' => 'admin', 'admin' => true)
1761+
);
1762+
Router::setRequestInfo(array(
1763+
array('controller' => 'posts', 'action' => 'index', 'admin' => true, 'prefix' => 'admin',
1764+
'form' => array(), 'url' => array(), 'plugin' => null
1765+
),
1766+
array('base' => '/', 'here' => '/', 'webroot' => '/')
1767+
));
1768+
$result = Router::url(array('controller' => 'users', 'action' => 'login', 'admin' => true));
1769+
$this->assertEqual($result, '/admin/login');
1770+
1771+
$result = Router::url(array('controller' => 'users', 'action' => 'login'));
1772+
$this->assertEqual($result, '/admin/login');
1773+
1774+
$result = Router::url(array('controller' => 'users', 'action' => 'admin_login'));
1775+
$this->assertEqual($result, '/admin/login');
1776+
}
1777+
17521778
/**
17531779
* testPassedArgsOrder method
17541780
*

0 commit comments

Comments
 (0)