diff --git a/lib/Cake/Routing/Route/CakeRoute.php b/lib/Cake/Routing/Route/CakeRoute.php index cc9b0ff73..b40529c9c 100644 --- a/lib/Cake/Routing/Route/CakeRoute.php +++ b/lib/Cake/Routing/Route/CakeRoute.php @@ -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); diff --git a/lib/Cake/Test/Case/Routing/Route/CakeRouteTest.php b/lib/Cake/Test/Case/Routing/Route/CakeRouteTest.php index 1f0e283ca..16bed4cd6 100644 --- a/lib/Cake/Test/Case/Routing/Route/CakeRouteTest.php +++ b/lib/Cake/Test/Case/Routing/Route/CakeRouteTest.php @@ -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); } + }