Permalink
Browse files

Expanding tests for passed arguments in route defaults working correc…

…tly. Refactoring passed arguments in route default handling.
  • Loading branch information...
1 parent 889e367 commit f59b8daf2e8b8c68a7f6fefcc8e0dca8d8a3bf68 @markstory markstory committed Nov 29, 2009
Showing with 22 additions and 27 deletions.
  1. +17 −27 cake/libs/router.php
  2. +5 −0 cake/tests/cases/libs/router.test.php
View
@@ -670,7 +670,7 @@ function promote($which = null) {
* Returns an URL pointing to a combination of controller and action. Param
* $url can be:
*
- * - Empty - the method will find adress to actuall controller/action.
+ * - Empty - the method will find address to actuall controller/action.
* - '/' - the method will find base URL of application.
* - A combination of controller/action - the method will find url for it.
*
@@ -693,7 +693,7 @@ function url($url = null, $full = false) {
if (is_bool($full)) {
$escape = false;
} else {
- extract(array_merge(array('escape' => false, 'full' => false), $full));
+ extract($full + array('escape' => false, 'full' => false));
}
if (!empty($self->__params)) {
@@ -751,17 +751,15 @@ function url($url = null, $full = false) {
unset($url[$prefix]);
}
}
- $plugin = false;
if (array_key_exists('plugin', $url)) {
- $plugin = $url['plugin'];
+ $params['plugin'] = $url['plugin'];
}
$_url = $url;
- $url = array_merge(array('controller' => $params['controller'], 'plugin' => $params['plugin']), Set::filter($url, true));
-
- if ($plugin !== false) {
- $url['plugin'] = $plugin;
- }
+ $url = array_merge(
+ array('controller' => $params['controller'], 'plugin' => $params['plugin']),
+ Set::filter($url, true)
+ );
if (isset($url['ext'])) {
$extension = '.' . $url['ext'];
@@ -771,8 +769,6 @@ function url($url = null, $full = false) {
for ($i = 0, $len = count($self->routes); $i < $len; $i++) {
$route =& $self->routes[$i];
- $route->compile();
-
$originalUrl = $url;
if (isset($route->params['persist'], $self->__params[0])) {
@@ -841,7 +837,7 @@ function url($url = null, $full = false) {
if (!empty($args)) {
$args = implode('/', $args);
if ($output{strlen($output) - 1} != '/') {
- $args = '/'. $args;
+ $args = '/' . $args;
}
$output .= $args;
}
@@ -944,8 +940,7 @@ function matchNamed($param, $val, $rule, $context = array()) {
if (!$actionMatches) {
return false;
}
- $valueMatches = !isset($rule['match']) || preg_match(sprintf('/%s/', $rule['match']), $val);
- return $valueMatches;
+ return (!isset($rule['match']) || preg_match('/' . $rule['match'] . '/', $val));
}
/**
@@ -1097,10 +1092,7 @@ function getArgs($args, $options = array()) {
$pass = $named = array();
$args = explode('/', $args);
- $greedy = $self->named['greedy'];
- if (isset($options['greedy'])) {
- $greedy = $options['greedy'];
- }
+ $greedy = isset($options['greedy']) ? $options['greedy'] : $self->named['greedy'];
$context = array();
if (isset($options['context'])) {
$context = $options['context'];
@@ -1363,19 +1355,17 @@ function match($url) {
$pass = array();
$params = Set::diff($url, $defaults);
- $urlInv = array_combine(array_values($url), array_keys($url));
- $i = 0;
- while (isset($defaults[$i])) {
- if (isset($urlInv[$defaults[$i]])) {
- if (!in_array($defaults[$i], $url) && is_int($urlInv[$defaults[$i]])) {
+ if (isset($defaults[0])) {
+ $i = 0;
+ while (isset($defaults[$i])) {
+ if (isset($url[$i]) && $defaults[$i] == $url[$i]) {
+ unset($defaults[$i]);
+ } else {
return false;
}
- unset($urlInv[$defaults[$i]], $defaults[$i]);
- } else {
- return false;
+ $i++;
}
- $i++;
}
foreach ($params as $key => $value) {
@@ -1764,6 +1764,7 @@ function testUrlWritingWithPrefixes() {
* @return void
*/
function testPassedArgsOrder() {
+ Router::connect('/test-passed/*', array('controller' => 'pages', 'action' => 'display', 'home'));
Router::connect('/test2/*', array('controller' => 'pages', 'action' => 'display', 2));
Router::connect('/test/*', array('controller' => 'pages', 'action' => 'display', 1));
Router::parse('/');
@@ -1776,6 +1777,10 @@ function testPassedArgsOrder() {
$expected = '/test2/whatever';
$this->assertEqual($result, $expected);
+ $result = Router::url(array('controller' => 'pages', 'action' => 'display', 'home', 'whatever'));
+ $expected = '/test-passed/whatever';
+ $this->assertEqual($result, $expected);
+
Configure::write('Routing.prefixes', array('admin'));
Router::reload();

0 comments on commit f59b8da

Please sign in to comment.