Permalink
Browse files

Sort route keys in reverse length order before replacing to prevent i…

…ncorrect matching
  • Loading branch information...
1 parent 5b7c3d6 commit 00956110f52c243d0f9cade9c03d5162427831fc Mike Gibson committed Mar 10, 2014
Showing with 17 additions and 1 deletion.
  1. +17 −1 lib/Cake/Routing/Route/CakeRoute.php
View
18 lib/Cake/Routing/Route/CakeRoute.php
@@ -518,7 +518,12 @@ protected function _writeUrl($params) {
$out = $this->template;
$search = $replace = array();
- foreach ($this->keys as $key) {
+ $keys = $this->keys;
+
+ // Sort the keys in reverse order by length to prevent mismatches
+ uasort($keys, array($this, '_sortKeys'));
+
+ foreach ($keys as $key) {
$string = null;
if (isset($params[$key])) {
$string = $params[$key];
@@ -537,4 +542,15 @@ protected function _writeUrl($params) {
return $out;
}
+ /**
+ * Comparison method for sorting keys in reverse order by length.
+ *
+ * @param $a
+ * @param $b
+ * @return int
+ */
+ protected function _sortKeys($a, $b) {
+ return strlen($a) > strlen($b) ? -1 : 1;
+ }
+
}

0 comments on commit 0095611

Please sign in to comment.