mirror of
https://gitee.com/fastadminnet/framework.git
synced 2026-07-09 16:12:49 +08:00
Route类改进 支持在匹配到路由后 使用after_behavior支持路由规则重定向
This commit is contained in:
@@ -25,7 +25,7 @@ class App
|
|||||||
* 执行应用程序
|
* 执行应用程序
|
||||||
* @access public
|
* @access public
|
||||||
* @param \think\Request $request Request对象
|
* @param \think\Request $request Request对象
|
||||||
* @return \think\Response
|
* @return mixed
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public static function run($request)
|
public static function run($request)
|
||||||
@@ -97,9 +97,12 @@ class App
|
|||||||
$data = self::invokeMethod($dispatch['method'], $dispatch['params']);
|
$data = self::invokeMethod($dispatch['method'], $dispatch['params']);
|
||||||
break;
|
break;
|
||||||
case 'function':
|
case 'function':
|
||||||
// 规则闭包
|
// 执行闭包
|
||||||
$data = self::invokeFunction($dispatch['function'], $dispatch['params']);
|
$data = self::invokeFunction($dispatch['function'], $dispatch['params']);
|
||||||
break;
|
break;
|
||||||
|
case 'response':
|
||||||
|
$data = $dispatch['response'];
|
||||||
|
break;
|
||||||
default:
|
default:
|
||||||
throw new Exception('dispatch type not support', 10008);
|
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');
|
$type = IS_AJAX ? Config::get('default_ajax_return') : Config::get('default_return_type');
|
||||||
return Response::create($data, $type)->send();
|
return Response::create($data, $type)->send();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 执行函数或者闭包方法 支持参数调用
|
// 执行函数或者闭包方法 支持参数调用
|
||||||
|
|||||||
@@ -510,6 +510,10 @@ class Route
|
|||||||
}
|
}
|
||||||
if (isset($miss)) {
|
if (isset($miss)) {
|
||||||
// 未匹配所有路由的路由规则处理
|
// 未匹配所有路由的路由规则处理
|
||||||
|
if ($miss instanceof \Closure) {
|
||||||
|
// 执行闭包
|
||||||
|
return ['type' => 'function', 'function' => $miss, 'params' => []];
|
||||||
|
}
|
||||||
if (self::checkOption($miss['option'], $url)) {
|
if (self::checkOption($miss['option'], $url)) {
|
||||||
return self::parseRule('', $miss['route'], $url, []);
|
return self::parseRule('', $miss['route'], $url, []);
|
||||||
}
|
}
|
||||||
@@ -603,9 +607,16 @@ class Route
|
|||||||
// 匹配到路由规则
|
// 匹配到路由规则
|
||||||
// 检测是否定义路由
|
// 检测是否定义路由
|
||||||
if (!empty($option['after_behavior'])) {
|
if (!empty($option['after_behavior'])) {
|
||||||
$result = Hook::exec($option['after_behavior'], $route);
|
if ($option['after_behavior'] instanceof \Closure) {
|
||||||
if (false === $result) {
|
$result = call_user_method_array($option['after_behavior'], [$route]);
|
||||||
return ['type' => 'finish'];
|
} 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) {
|
if ($route instanceof \Closure) {
|
||||||
|
|||||||
Reference in New Issue
Block a user