mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2025-03-28 20:52:59 +00:00
Adding fixes for reverse routing engine
git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@4076 3807eeeb-6ff5-0310-8944-8be069107fe0
This commit is contained in:
parent
403ff35bdb
commit
94a57a89a4
1 changed files with 71 additions and 39 deletions
|
@ -102,9 +102,11 @@ class Router extends Overloadable {
|
||||||
if (defined('CAKE_ADMIN')) {
|
if (defined('CAKE_ADMIN')) {
|
||||||
$admin = CAKE_ADMIN;
|
$admin = CAKE_ADMIN;
|
||||||
if (!empty($admin)) {
|
if (!empty($admin)) {
|
||||||
$this->__admin = array('/:' . $admin . '/:controller/:action/* (default)',
|
$this->__admin = array(
|
||||||
|
'/:' . $admin . '/:controller/:action/*',
|
||||||
'/^(?:\/(?:(' . $admin . ')(?:\\/([a-zA-Z0-9_\\-\\.\\;\\:]+)(?:\\/([a-zA-Z0-9_\\-\\.\\;\\:]+)(?:[\\/\\?](.*))?)?)?))[\/]*$/',
|
'/^(?:\/(?:(' . $admin . ')(?:\\/([a-zA-Z0-9_\\-\\.\\;\\:]+)(?:\\/([a-zA-Z0-9_\\-\\.\\;\\:]+)(?:[\\/\\?](.*))?)?)?))[\/]*$/',
|
||||||
array($admin, 'controller', 'action'), array());
|
array($admin, 'controller', 'action'), array()
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -119,7 +121,6 @@ class Router extends Overloadable {
|
||||||
if (!isset($instance[0]) || !$instance[0]) {
|
if (!isset($instance[0]) || !$instance[0]) {
|
||||||
$instance[0] =& new Router();
|
$instance[0] =& new Router();
|
||||||
}
|
}
|
||||||
|
|
||||||
return $instance[0];
|
return $instance[0];
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
|
@ -182,7 +183,7 @@ class Router extends Overloadable {
|
||||||
|
|
||||||
if (preg_match('/^:(.+)$/', $element, $r)) {
|
if (preg_match('/^:(.+)$/', $element, $r)) {
|
||||||
if (isset($params[$r[1]])) {
|
if (isset($params[$r[1]])) {
|
||||||
if (array_key_exists($r[1], $default) && $default[$r[1]] == null) {
|
if (array_key_exists($r[1], $default)) {
|
||||||
$q = '?';
|
$q = '?';
|
||||||
}
|
}
|
||||||
$parsed[] = '(?:\/(' . $params[$r[1]] . '))' . $q;
|
$parsed[] = '(?:\/(' . $params[$r[1]] . '))' . $q;
|
||||||
|
@ -219,9 +220,11 @@ class Router extends Overloadable {
|
||||||
}
|
}
|
||||||
$out = array();
|
$out = array();
|
||||||
$r = null;
|
$r = null;
|
||||||
$default_route = array('/:controller/:action/* (default)',
|
$default_route = array(
|
||||||
|
'/:controller/:action/*',
|
||||||
'/^(?:\/(?:([a-zA-Z0-9_\\-\\.\\;\\:]+)(?:\\/([a-zA-Z0-9_\\-\\.\\;\\:]+)(?:[\\/\\?](.*))?)?))[\\/]*$/',
|
'/^(?:\/(?:([a-zA-Z0-9_\\-\\.\\;\\:]+)(?:\\/([a-zA-Z0-9_\\-\\.\\;\\:]+)(?:[\\/\\?](.*))?)?))[\\/]*$/',
|
||||||
array('controller', 'action'), array());
|
array('controller', 'action'), array()
|
||||||
|
);
|
||||||
|
|
||||||
if (defined('CAKE_ADMIN') && $_this->__admin != null) {
|
if (defined('CAKE_ADMIN') && $_this->__admin != null) {
|
||||||
$_this->routes[] = $_this->__admin;
|
$_this->routes[] = $_this->__admin;
|
||||||
|
@ -300,11 +303,7 @@ class Router extends Overloadable {
|
||||||
|
|
||||||
$_this =& Router::getInstance();
|
$_this =& Router::getInstance();
|
||||||
$params[1] = am(
|
$params[1] = am(
|
||||||
array(
|
array('plugin' => null, 'controller' => null, 'action' => null),
|
||||||
'plugin' => null,
|
|
||||||
'controller' => null,
|
|
||||||
'action' => null
|
|
||||||
),
|
|
||||||
$params[1]
|
$params[1]
|
||||||
);
|
);
|
||||||
list($_this->__params[], $_this->__paths[]) = $params;
|
list($_this->__params[], $_this->__paths[]) = $params;
|
||||||
|
@ -368,10 +367,7 @@ class Router extends Overloadable {
|
||||||
$params = isset($_this->__params[0]) ? $_this->__params[0] : array();
|
$params = isset($_this->__params[0]) ? $_this->__params[0] : array();
|
||||||
$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 = $output = $mapped = $q = null;
|
||||||
$output = 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) {
|
||||||
|
@ -391,12 +387,10 @@ class Router extends Overloadable {
|
||||||
$url['action'] = 'index';
|
$url['action'] = 'index';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (!isset($url['controller'])) {
|
$url = am(
|
||||||
$url['controller'] = $params['controller'];
|
array('controller' => $params['controller'], 'plugin' => $params['plugin']),
|
||||||
}
|
$url
|
||||||
if (!isset($url['plugin'])) {
|
);
|
||||||
$url['plugin'] = $params['plugin'];
|
|
||||||
}
|
|
||||||
if (isset($url['ext'])) {
|
if (isset($url['ext'])) {
|
||||||
$extension = '.' . $url['ext'];
|
$extension = '.' . $url['ext'];
|
||||||
}
|
}
|
||||||
|
@ -492,27 +486,43 @@ class Router extends Overloadable {
|
||||||
* @return mixed
|
* @return mixed
|
||||||
*/
|
*/
|
||||||
function mapRouteElements($route, $url) {
|
function mapRouteElements($route, $url) {
|
||||||
$url = am(array('action' => 'index'), $url);
|
$params = $route[2];
|
||||||
$route[3] = am(array('action' => 'index'), $route[3]);
|
$defaults = am(array('plugin' => null), $route[3]);
|
||||||
$elements = array_diff_assoc($url, $route[3]);
|
$required = array_diff_key($defaults, $params);
|
||||||
$diffs = array_diff_assoc($route[3], $url);
|
$url = am(array('action' => 'index', 'plugin' => null), $url);
|
||||||
$diffed = array_keys($diffs);
|
|
||||||
|
foreach ($defaults as $key => $val) {
|
||||||
|
if (!is_numeric($key) && !isset($url[$key])) {
|
||||||
|
$url[$key] = $val;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if ($route[3] == $url) {
|
if ($route[3] == $url) {
|
||||||
return array($route[0], array());
|
return array(Router::__mapRoute($route, $url), array());
|
||||||
} elseif (!empty($diffs)) {
|
} elseif (!empty($params)) {
|
||||||
|
$filled = array_intersect_key($url, array_combine($params, array_keys($params)));
|
||||||
|
$keysFilled = array_keys($filled);
|
||||||
|
sort($params);
|
||||||
|
sort($keysFilled);
|
||||||
|
if ($keysFilled != $params) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/*if (!empty($diffs)) {
|
||||||
if (isset($route[3]['controller']) && in_array('controller', $diffed)) {
|
if (isset($route[3]['controller']) && in_array('controller', $diffed)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
if (isset($route[3]['action']) && in_array('action', $diffed)) {
|
if (isset($route[3]['action']) && in_array('action', $diffed)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}*/
|
||||||
|
|
||||||
$required = array_diff(array_diff($route[2], array_keys($route[3])), array_keys($url));
|
//$required = array_diff(array_diff($route[2], array_keys($route[3])), array_keys($url));
|
||||||
|
/*sort($required);
|
||||||
if (!empty($required)) {
|
if (!empty($required)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}*/
|
||||||
|
|
||||||
foreach ($route[4] as $key => $reg) {
|
foreach ($route[4] as $key => $reg) {
|
||||||
if (isset($url[$key]) && !preg_match('/' . $reg . '/', $url[$key])) {
|
if (isset($url[$key]) && !preg_match('/' . $reg . '/', $url[$key])) {
|
||||||
|
@ -520,13 +530,35 @@ class Router extends Overloadable {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$out = str_replace(array('/bare', '/*', '/ajax'), '', $route[0]);
|
//$out = str_replace(array('/bare', '/*', '/ajax'), '', $route[0]);
|
||||||
foreach ($route[2] as $key) {
|
return array(Router::__mapRoute($route, $url), $url);
|
||||||
$out = str_replace(':' . $key, $url[$key], $out);
|
|
||||||
unset($url[$key]);
|
|
||||||
}
|
}
|
||||||
$_this =& Router::getInstance();
|
/**
|
||||||
return array($out, $url);
|
* Merges URL parameters into a route string
|
||||||
|
*
|
||||||
|
* @param array Route
|
||||||
|
* @param array $params
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
function __mapRoute($route, $params = array()) {
|
||||||
|
if (isset($params['pass']) && is_array($params['pass'])) {
|
||||||
|
$params['pass'] = implode('/', array_filter($params['pass']));
|
||||||
|
} elseif (!isset($params['pass'])) {
|
||||||
|
$params['pass'] = '';
|
||||||
|
}
|
||||||
|
|
||||||
|
if (strpos($route[0], '*')) {
|
||||||
|
$out = str_replace('*', $params['pass'], $route[0]);
|
||||||
|
} else {
|
||||||
|
$out = $route[0] . $params['pass'];
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach ($route[2] as $key) {
|
||||||
|
$out = str_replace(':' . $key, $params[$key], $out);
|
||||||
|
unset($params[$key]);
|
||||||
|
}
|
||||||
|
// Do something else here for leftover params
|
||||||
|
return $out;
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
* Generates a well-formed querystring from $q
|
* Generates a well-formed querystring from $q
|
||||||
|
|
Loading…
Add table
Reference in a new issue