Skip to content

Commit

Permalink
Merge pull request #2991 from mikegibson/route_params
Browse files Browse the repository at this point in the history
Fixed incorrect replacement of route elements beginning with same string
  • Loading branch information
markstory committed Mar 11, 2014
2 parents 47900c1 + 0de1307 commit 0c207db
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 10 deletions.
30 changes: 20 additions & 10 deletions lib/Cake/Routing/Route/CakeRoute.php
Expand Up @@ -517,18 +517,28 @@ protected function _writeUrl($params) {
}
$out = $this->template;

$search = $replace = array();
foreach ($this->keys as $key) {
$string = null;
if (isset($params[$key])) {
$string = $params[$key];
} elseif (strpos($out, $key) != strlen($out) - strlen($key)) {
$key .= '/';
if ($this->keys !== array()) {

$search = $replace = array();

$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];
} elseif (strpos($out, $key) != strlen($out) - strlen($key)) {
$key .= '/';
}
$search[] = ':' . $key;
$replace[] = $string;
}
$search[] = ':' . $key;
$replace[] = $string;
$out = str_replace($search, $replace, $out);

}
$out = str_replace($search, $replace, $out);

if (strpos($this->template, '*')) {
$out = str_replace('*', $params['pass'], $out);
Expand Down
19 changes: 19 additions & 0 deletions lib/Cake/Test/Case/Routing/Route/CakeRouteTest.php
Expand Up @@ -855,6 +855,24 @@ public function testMatchNamedParametersArray() {
$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
*
Expand Down Expand Up @@ -941,4 +959,5 @@ public function testUTF8PatternOnSection() {
$expected = array('section' => 'weblog', 'plugin' => 'blogs', 'controller' => 'posts', 'action' => 'index', 'pass' => array(), 'named' => array());
$this->assertEquals($expected, $result);
}

}

0 comments on commit 0c207db

Please sign in to comment.