Skip to content

Commit

Permalink
Sort route keys in reverse length order before replacing to prevent i…
Browse files Browse the repository at this point in the history
…ncorrect matching
  • Loading branch information
mgdigital committed Mar 10, 2014
1 parent 5b7c3d6 commit 0095611
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion lib/Cake/Routing/Route/CakeRoute.php
Expand Up @@ -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];
Expand All @@ -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.