路由优化 支持[模块/控制器/操作/参数1/值1/参数2/值2...]路由模式

This commit is contained in:
huangdijia
2015-12-04 13:40:24 +08:00
parent bc46e9ccca
commit 3126a6d02d
2 changed files with 15 additions and 6 deletions

View File

@@ -266,25 +266,27 @@ class App
// URL后缀
define('__EXT__', strtolower(pathinfo($_SERVER['PATH_INFO'], PATHINFO_EXTENSION)));
$_SERVER['PATH_INFO'] = __INFO__;
if (__INFO__ && !defined('BIND_MODULE')) {
if (__INFO__) {
if ($config['url_deny_suffix'] && preg_match('/\.(' . $config['url_deny_suffix'] . ')$/i', __INFO__)) {
throw new Exception('URL_SUFFIX_DENY');
}
// 还原劫持后真实pathinfo
$path_info = (defined('BIND_MODULE')?BIND_MODULE.'/':'') . (defined('BIND_CONTROLLER')?BIND_CONTROLLER.'/':'') . (defined('BIND_ACTION')?BIND_ACTION.'/':'') . __INFO__;
// 路由检测
if (!empty($config['url_route_on'])) {
// 开启路由 则检测路由配置
Route::register($config['route']);
$result = Route::check(__INFO__, $config['pathinfo_depr']);
$result = Route::check($path_info, $config['pathinfo_depr']);
if (false === $result) {
// 路由无效
if ($config['url_route_must']) {
throw new Exception('route not define ');
} else {
$result = Route::parseUrl(__INFO__);
$result = Route::parseUrl($path_info);
}
}
} else {
$result = Route::parseUrl(__INFO__);
$result = Route::parseUrl($path_info);
}
}
// 去除URL后缀

View File

@@ -435,20 +435,27 @@ class Route
if (false !== strpos($url, '?')) {
// [控制器/操作?]参数1=值1&参数2=值2...
$info = parse_url($url);
$path = explode('/', $info['path']);
$path = explode('/', $info['path'], 4);
parse_str($info['query'], $var);
} elseif (strpos($url, '/')) {
// [控制器/操作]
$path = explode('/', $url);
$path = explode('/', $url, 4);
} else {
// 参数1=值1&参数2=值2...
parse_str($url, $var);
}
if (isset($path)) {
$param = [];
if(!empty($path[3])) $param = explode('/', array_pop($path));
// 解析[模块/控制器/操作]
$action = array_pop($path);
$action = '[rest]' == $action ? REQUEST_METHOD : $action;
$controller = !empty($path) ? array_pop($path) : null;
$module = !empty($path) ? array_pop($path) : null;
// 解析path额外的参数
for ($i=0; $i<count($param);$i++) {
$var[$param[$i]] = $param[++$i];
}
}
return ['route' => [$module, $controller, $action], 'var' => $var];
}