Moving strip_plugin() to Router::stripPlugin(), and enabling reverse routes

git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@3854 3807eeeb-6ff5-0310-8944-8be069107fe0
This commit is contained in:
nate 2006-11-10 17:08:31 +00:00
parent 3ca4762d32
commit 6ba7032e68
4 changed files with 111 additions and 72 deletions

View file

@ -1199,12 +1199,11 @@
} }
} }
/** /**
* Counts the dimensions of an array * @deprecated
* * @see Set::countDim
* @param array $array
* @return int The number of dimensions in $array
*/ */
function countdim($array) { function countdim($array) {
trigger_error('Deprecated: Use Set::countDim instead', E_USER_WARNING);
if (is_array(reset($array))) { if (is_array(reset($array))) {
$return = countdim(reset($array)) + 1; $return = countdim(reset($array)) + 1;
} else { } else {
@ -1299,24 +1298,4 @@
return false; return false;
} }
} }
/**
* removed the plugin name from the base url
*
* @param string $base
* @param string $plugin
* @return base url with plugin name removed if present
*/
function strip_plugin($base, $plugin){
if ($plugin != null) {
$base = preg_replace('/' . $plugin . '/', '', $base);
$base = str_replace('//', '', $base);
$pos1 = strrpos($base, '/');
$char = strlen($base) - 1;
if ($pos1 == $char) {
$base = substr($base, 0, $char);
}
}
return $base;
}
?> ?>

View file

@ -194,7 +194,7 @@ class Dispatcher extends Object {
} }
$controller->base = $this->base; $controller->base = $this->base;
$base = strip_plugin($this->base, $this->plugin); $base = Router::stripPlugin($this->base, $this->plugin);
if(defined("BASE_URL")) { if(defined("BASE_URL")) {
$controller->here = $base . $this->admin . $url; $controller->here = $base . $this->admin . $url;
} else { } else {

View file

@ -364,7 +364,9 @@ class Router extends Overloadable {
$path = isset($_this->__paths[0]) ? $_this->__paths[0] : array(); $path = isset($_this->__paths[0]) ? $_this->__paths[0] : array();
$base = $_this->stripPlugin($path['base'], $params['plugin']); $base = $_this->stripPlugin($path['base'], $params['plugin']);
$extension = null; $extension = null;
$output = null;
$mapped = null; $mapped = null;
$q = null;
if (is_array($url) && !empty($url)) { if (is_array($url) && !empty($url)) {
if (isset($url['full_base']) && $url['full_base'] == true) { if (isset($url['full_base']) && $url['full_base'] == true) {
@ -372,6 +374,11 @@ class Router extends Overloadable {
unset($url['full_base']); unset($url['full_base']);
} }
if (isset($url['?'])) {
$q = $url['?'];
unset($url['?']);
}
if (!isset($url['action'])) { if (!isset($url['action'])) {
if (!isset($url['controller']) || $params['controller'] == $url['controller']) { if (!isset($url['controller']) || $params['controller'] == $url['controller']) {
$url['action'] = $params['action']; $url['action'] = $params['action'];
@ -392,6 +399,17 @@ class Router extends Overloadable {
$url[CAKE_ADMIN] = $params['admin']; $url[CAKE_ADMIN] = $params['admin'];
} }
foreach ($_this->routes as $route) {
$match = $_this->mapRouteElements($route, $url);
if ($match !== false) {
list($output, $url) = $match;
if ($output{0} == '/') {
$output = substr($output, 1);
}
break;
}
}
$named = $args = array(); $named = $args = array();
$keys = array_keys($url); $keys = array_keys($url);
$count = count($keys); $count = count($keys);
@ -408,32 +426,10 @@ class Router extends Overloadable {
} }
} }
if ($match === false) {
if (empty($named) && empty($args) && $url['action'] == 'index') { if (empty($named) && empty($args) && $url['action'] == 'index') {
$url['action'] = null; $url['action'] = null;
} }
foreach ($_this->routes as $route) {
$diff = array_diff_assoc($url, $route[3]);
if (empty($diff)) {
$match = true;
foreach ($route[4] as $key => $pattern) {
if (isset($url[$key])) {
if (!preg_match($pattern, $url[$key])) {
$match = false;
break;
}
} elseif (isset($route[2]) && $route[2] == null) {
$match = false;
break;
}
}
if ($match) {
$mapped = $_this->mapRouteElements($route, $url);
foreach (array_keys($route[3]) as $key) {
$url[$key] = '';
}
}
}
} }
$combined = ''; $combined = '';
@ -454,17 +450,17 @@ class Router extends Overloadable {
$combined = join('/', $named); $combined = join('/', $named);
} }
if (isset($url['?']) && !empty($url['?'])) { if ($match === false) {
$url['?'] = '?' . $url['?'];
} else {
$url['?'] = null;
}
$urlOut = array_filter(array($url['plugin'], $url['controller'], $url['action'], join('/', array_filter($args)), $combined)); $urlOut = array_filter(array($url['plugin'], $url['controller'], $url['action'], join('/', array_filter($args)), $combined));
if (defined('CAKE_ADMIN') && isset($url[CAKE_ADMIN]) && $url[CAKE_ADMIN]) { if (defined('CAKE_ADMIN') && isset($url[CAKE_ADMIN]) && $url[CAKE_ADMIN]) {
array_unshift($urlOut, CAKE_ADMIN); array_unshift($urlOut, CAKE_ADMIN);
} }
$output = $base . '/' . join('/', $urlOut); $output = join('/', $urlOut);
} elseif (!empty($combined)) {
$output .= '/' . $combined;
}
$output = $base . '/' . $output;
} else { } else {
if (((strpos($url, '://')) || (strpos($url, 'javascript:') === 0) || (strpos($url, 'mailto:') === 0)) || (strpos($url, '#') === 0)) { if (((strpos($url, '://')) || (strpos($url, 'javascript:') === 0) || (strpos($url, 'mailto:') === 0)) || (strpos($url, '#') === 0)) {
return $url; return $url;
@ -481,7 +477,7 @@ class Router extends Overloadable {
if ($full) { if ($full) {
$output = FULL_BASE_URL . $output; $output = FULL_BASE_URL . $output;
} }
return $output . $extension; return $output . $extension . $_this->queryString($q);
} }
/** /**
* Maps a URL array onto a route and returns the string result, or false if no match * Maps a URL array onto a route and returns the string result, or false if no match
@ -495,21 +491,51 @@ class Router extends Overloadable {
$route[3] = am(array('action' => 'index'), $route[3]); $route[3] = am(array('action' => 'index'), $route[3]);
$elements = array_diff_assoc($url, $route[3]); $elements = array_diff_assoc($url, $route[3]);
$diffs = array_diff_assoc($route[3], $url); $diffs = array_diff_assoc($route[3], $url);
$match = false; $diffed = array_keys($diffs);
if ($route[3] === $url) {
return $route[0];
} elseif (empty($diffs)) {
if ($route[3] == $url) {
return array($route[0], array());
} elseif (!empty($diffs)) {
if (isset($route[3]['controller']) && in_array('controller', $diffed)) {
return false;
}
if (isset($route[3]['action']) && in_array('action', $diffed)) {
return false;
}
} }
// if ($match == false) { ... } $required = array_diff(array_diff($route[2], array_keys($route[3])), array_keys($url));
if (!empty($required)) {
if ($match) { return false;
} else {
$url = false;
} }
foreach ($route[4] as $key => $reg) {
if (isset($url[$key]) && !preg_match('/' . $reg . '/', $url[$key])) {
pr($route[0].':break4');
return false;
}
}
$out = str_replace(array('/bare', '/*', '/ajax'), '', $route[0]);
foreach ($route[2] as $key) {
$out = str_replace(':' . $key, $url[$key], $out);
unset($url[$key]);
}
$_this =& Router::getInstance();
return array($out, $url);
}
/**
* Generates a well-formed querystring from $q
*
* @param mixed Querystring
* @param array Extra querystring parameters
* @return array
*/
function queryString($q, $extra = array()) {
if (empty($q)) {
return null;
}
return '?' . $q;
} }
/** /**
* Returns the route matching the current request URL * Returns the route matching the current request URL

View file

@ -207,13 +207,47 @@ class Set extends Object {
} }
return $data; return $data;
} }
/**
* Determines if two Sets or arrays are equal
*
* @param array $val1
* @param array $val2
* @return boolean
*/
function isEqual($val1, $val2 = null) { function isEqual($val1, $val2 = null) {
if ($val2 == null && (is_a($this, 'set') || is_a($this, 'Set'))) { if ($val2 == null && (is_a($this, 'set') || is_a($this, 'Set'))) {
$val2 = $val1; $val2 = $val1;
$val1 = $this->get(); $val1 = $this->get();
} }
} }
/**
* Determines if one Set or array contains the exact keys and values of another.
*
* @param array $val1
* @param array $val2
* @return boolean
*/
function contains($val1, $val2 = null) {
if ($val2 == null && is_a($this, 'set')) {
$val2 = $val1;
$val1 = $this->get();
} elseif ($val2 != null && is_object($val2) && is_a($val2, 'set')) {
$val2 = $val2->get();
}
foreach ($val2 as $key => $val) {
if (is_numeric($key)) {
if (!in_array($val, $val1)) {
return false;
}
} else {
if (!isset($val1[$key]) || $val1[$key] != $val) {
return false;
}
}
}
return true;
}
/** /**
* Counts the dimensions of an array * Counts the dimensions of an array
* *