Route类改进 支持在匹配到路由后 使用after_behavior支持路由规则重定向

This commit is contained in:
thinkphp
2016-05-17 16:01:47 +08:00
parent 0acf217660
commit 3544809b1d
2 changed files with 19 additions and 6 deletions

View File

@@ -25,7 +25,7 @@ class App
* 执行应用程序
* @access public
* @param \think\Request $request Request对象
* @return \think\Response
* @return mixed
* @throws Exception
*/
public static function run($request)
@@ -97,9 +97,12 @@ class App
$data = self::invokeMethod($dispatch['method'], $dispatch['params']);
break;
case 'function':
// 规则闭包
// 执行闭包
$data = self::invokeFunction($dispatch['function'], $dispatch['params']);
break;
case 'response':
$data = $dispatch['response'];
break;
default:
throw new Exception('dispatch type not support', 10008);
}
@@ -117,7 +120,6 @@ class App
$type = IS_AJAX ? Config::get('default_ajax_return') : Config::get('default_return_type');
return Response::create($data, $type)->send();
}
}
// 执行函数或者闭包方法 支持参数调用

View File

@@ -510,6 +510,10 @@ class Route
}
if (isset($miss)) {
// 未匹配所有路由的路由规则处理
if ($miss instanceof \Closure) {
// 执行闭包
return ['type' => 'function', 'function' => $miss, 'params' => []];
}
if (self::checkOption($miss['option'], $url)) {
return self::parseRule('', $miss['route'], $url, []);
}
@@ -603,9 +607,16 @@ class Route
// 匹配到路由规则
// 检测是否定义路由
if (!empty($option['after_behavior'])) {
$result = Hook::exec($option['after_behavior'], $route);
if (false === $result) {
return ['type' => 'finish'];
if ($option['after_behavior'] instanceof \Closure) {
$result = call_user_method_array($option['after_behavior'], [$route]);
} else {
$result = Hook::exec($option['after_behavior'], $route);
}
// 路由规则重定向
if ($result instanceof Response) {
return ['type' => 'response', 'response' => $result, 'params' => $match];
} elseif (is_array($result)) {
return $result;
}
}
if ($route instanceof \Closure) {