Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge pull request #2991 from mikegibson/route_params

Fixed incorrect replacement of route elements beginning with same string
  • Loading branch information...
commit 0c207dba4b5972cdccb6f42ac76e1173189beb5f 2 parents 47900c1 + 0de1307
Mark Story markstory authored
30 lib/Cake/Routing/Route/CakeRoute.php
View
@@ -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);
19 lib/Cake/Test/Case/Routing/Route/CakeRouteTest.php
View
@@ -856,6 +856,24 @@ public function testMatchNamedParametersArray() {
}
/**
+ * 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
*
* @return void
@@ -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);
}
+
}
Please sign in to comment.
Something went wrong with that request. Please try again.