Merge pull request #2991 from mikegibson/route_params

Fixed incorrect replacement of route elements beginning with same string
This commit is contained in:
Mark Story 2014-03-10 21:28:38 -04:00
commit 0c207dba4b
2 changed files with 39 additions and 10 deletions

View file

@ -517,8 +517,16 @@ class CakeRoute {
}
$out = $this->template;
if ($this->keys !== array()) {
$search = $replace = array();
foreach ($this->keys as $key) {
$lengths = array_map('strlen', $this->keys);
$flipped = array_combine($this->keys, $lengths);
arsort($flipped);
$keys = array_keys($flipped);
foreach ($keys as $key) {
$string = null;
if (isset($params[$key])) {
$string = $params[$key];
@ -530,6 +538,8 @@ class CakeRoute {
}
$out = str_replace($search, $replace, $out);
}
if (strpos($this->template, '*')) {
$out = str_replace('*', $params['pass'], $out);
}

View file

@ -855,6 +855,24 @@ class CakeRouteTest extends CakeTestCase {
$this->assertEquals($expected, $result);
}
/**
* Test matching of parameters where one parameter name starts with another parameter name
*
* @return void
*/
public function testMatchSimilarParameters() {
$route = new CakeRoute('/:thisParam/:thisParamIsLonger');
$url = array(
'thisParam' => 'foo',
'thisParamIsLonger' => 'bar'
);
$result = $route->match($url);
$expected = '/foo/bar';
$this->assertEquals($expected, $result);
}
/**
* test restructuring args with pass key
*
@ -941,4 +959,5 @@ class CakeRouteTest extends CakeTestCase {
$expected = array('section' => 'weblog', 'plugin' => 'blogs', 'controller' => 'posts', 'action' => 'index', 'pass' => array(), 'named' => array());
$this->assertEquals($expected, $result);
}
}