From 00956110f52c243d0f9cade9c03d5162427831fc Mon Sep 17 00:00:00 2001 From: Mike Gibson Date: Mon, 10 Mar 2014 11:43:28 +0000 Subject: [PATCH] Sort route keys in reverse length order before replacing to prevent incorrect matching --- lib/Cake/Routing/Route/CakeRoute.php | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/lib/Cake/Routing/Route/CakeRoute.php b/lib/Cake/Routing/Route/CakeRoute.php index cc9b0ff73..a91f509f1 100644 --- a/lib/Cake/Routing/Route/CakeRoute.php +++ b/lib/Cake/Routing/Route/CakeRoute.php @@ -518,7 +518,12 @@ class CakeRoute { $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 @@ class CakeRoute { 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; + } + }