mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2025-01-31 09:06:17 +00:00
Merge pull request #2991 from mikegibson/route_params
Fixed incorrect replacement of route elements beginning with same string
This commit is contained in:
commit
0c207dba4b
2 changed files with 39 additions and 10 deletions
|
@ -517,18 +517,28 @@ class CakeRoute {
|
|||
}
|
||||
$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);
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue