From 3544809b1d162e180ab6447fdad577846b43d473 Mon Sep 17 00:00:00 2001 From: thinkphp Date: Tue, 17 May 2016 16:01:47 +0800 Subject: [PATCH] =?UTF-8?q?Route=E7=B1=BB=E6=94=B9=E8=BF=9B=20=E6=94=AF?= =?UTF-8?q?=E6=8C=81=E5=9C=A8=E5=8C=B9=E9=85=8D=E5=88=B0=E8=B7=AF=E7=94=B1?= =?UTF-8?q?=E5=90=8E=20=E4=BD=BF=E7=94=A8after=5Fbehavior=E6=94=AF?= =?UTF-8?q?=E6=8C=81=E8=B7=AF=E7=94=B1=E8=A7=84=E5=88=99=E9=87=8D=E5=AE=9A?= =?UTF-8?q?=E5=90=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/App.php | 8 +++++--- library/think/Route.php | 17 ++++++++++++++--- 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/library/think/App.php b/library/think/App.php index f4d1404c..3c6440db 100644 --- a/library/think/App.php +++ b/library/think/App.php @@ -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(); } - } // 执行函数或者闭包方法 支持参数调用 diff --git a/library/think/Route.php b/library/think/Route.php index b2481ff1..f87cf841 100644 --- a/library/think/Route.php +++ b/library/think/Route.php @@ -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) {