修正route类的parseURL解析

This commit is contained in:
thinkphp
2015-12-08 16:59:06 +08:00
parent 003b99cdd6
commit 08d118a154
3 changed files with 33 additions and 14 deletions

View File

@@ -71,7 +71,7 @@ class App
// 执行操作
if (!preg_match('/^[A-Za-z](\/|\.|\w)*$/', CONTROLLER_NAME)) {
// 安全检测
throw new Exception('[ ' . MODULE_NAME . '\\' . CONTROLLER_LAYER . '\\' . Loader::parseName(str_replace('.', '\\', CONTROLLER_NAME), 1) . ' ] not exists');
throw new Exception('class [ ' . MODULE_NAME . '\\' . CONTROLLER_LAYER . '\\' . Loader::parseName(str_replace('.', '\\', CONTROLLER_NAME), 1) . ' ] not exists');
}
if ($config['action_bind_class']) {
$class = self::bindActionClass($config['empty_controller']);
@@ -84,6 +84,9 @@ class App
$action = ACTION_NAME . $config['action_suffix'];
}
if (!$instance) {
throw new Exception('class [ ' . MODULE_NAME . '\\' . CONTROLLER_LAYER . '\\' . Loader::parseName(str_replace('.', '\\', CONTROLLER_NAME), 1) . ' ] not exists');
}
try {
// 操作方法开始监听
$call = [$instance, $action];
@@ -119,7 +122,7 @@ class App
$method = new \ReflectionMethod($instance, '_empty');
$method->invokeArgs($instance, [$action, '']);
} else {
throw new Exception('[ ' . (new \ReflectionClass($instance))->getName() . ':' . $action . ' ] not exists ', 404);
throw new Exception('method [ ' . (new \ReflectionClass($instance))->getName() . '->' . $action . ' ] not exists ');
}
}
}

View File

@@ -125,10 +125,16 @@ class Error
}
}
}
// 异常信息输出监听
Hook::listen('error_output', $e);
// 输出异常内容
Response::returnData($e, Config::get('default_return_type'));
$type = Config::get('default_return_type');
if ('html' == $type) {
include Config::get('exception_tmpl');
} else {
// 异常信息输出监听
Hook::listen('error_output', $e);
// 输出异常内容
Response::returnData($e, $type);
}
exit;
}
}

View File

@@ -424,7 +424,7 @@ class Route
if ('/' != $depr) {
$url = str_replace($depr, '/', $url);
}
$result = self::parseRoute($url);
$result = self::parseRoute($url, true);
if (!empty($result['var'])) {
$_GET = array_merge($result['var'], $_GET);
}
@@ -433,7 +433,7 @@ class Route
// 解析规范的路由地址
// 地址格式 [模块/控制器/操作?]参数1=值1&参数2=值2...
private static function parseRoute($url)
private static function parseRoute($url, $reverse = false)
{
$var = [];
if (false !== strpos($url, '?')) {
@@ -444,10 +444,13 @@ class Route
} elseif (strpos($url, '/')) {
// [模块/控制器/操作]
$path = explode('/', $url, 4);
} else {
} elseif (false !== strpos($url, '=')) {
// 参数1=值1&参数2=值2...
parse_str($url, $var);
} else {
$path = [$url];
}
$route = [null, null, null];
if (isset($path)) {
// 解析path额外的参数
if (!empty($path[3])) {
@@ -456,12 +459,19 @@ class Route
}, 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;
if ($reverse) {
$module = array_shift($path);
$controller = !empty($path) ? array_shift($path) : null;
$action = !empty($path) ? array_shift($path) : null;
} else {
$action = array_pop($path);
$controller = !empty($path) ? array_pop($path) : null;
$module = !empty($path) ? array_pop($path) : null;
}
$action = '[rest]' == $action ? REQUEST_METHOD : $action;
$route = [$module, $controller, $action];
}
return ['route' => [$module, $controller, $action], 'var' => $var];
return ['route' => $route, 'var' => $var];
}
// 检测URL和规则路由是否匹配