中間件
中間件一般用于攔截請(qǐng)求或者響應(yīng)。例如執(zhí)行控制器前統(tǒng)一驗(yàn)證用戶身份,如用戶未登錄時(shí)跳轉(zhuǎn)到登錄頁(yè)面,例如響應(yīng)中增加某個(gè)header頭。例如統(tǒng)計(jì)某個(gè)uri請(qǐng)求占比等等。
中間件洋蔥模型
┌──────────────────────────────────────────────────────┐
│ middleware1 │
│ ┌──────────────────────────────────────────┐ │
│ │ middleware2 │ │
│ │ ┌──────────────────────────────┐ │ │
│ │ │ middleware3 │ │ │
│ │ │ ┌──────────────────┐ │ │ │
│ │ │ │ │ │ │ │
── Reqeust ───────────────────> Controller ── Response ───────────────────────────> Client
│ │ │ │ │ │ │ │
│ │ │ └──────────────────┘ │ │ │
│ │ │ │ │ │
│ │ └──────────────────────────────┘ │ │
│ │ │ │
│ └──────────────────────────────────────────┘ │
│ │
└──────────────────────────────────────────────────────┘
中間件和控制器組成了一個(gè)經(jīng)典的洋蔥模型,中間件類似一層一層的洋蔥表皮,控制器是洋蔥芯。如圖所示請(qǐng)求像箭一樣穿越中間件1、2、3到達(dá)控制器,控制器返回了一個(gè)響應(yīng),然后響應(yīng)又以3、2、1的順序穿出中間件最終返回給客戶端。也就是說(shuō)在每個(gè)中間件里我們既可以拿到請(qǐng)求,也可以獲得響應(yīng)。
請(qǐng)求攔截
有時(shí)候我們不想某個(gè)請(qǐng)求到達(dá)控制器層,例如我們?cè)趍iddleware2發(fā)現(xiàn)當(dāng)前用戶并沒有登錄,則我們可以直接攔截請(qǐng)求并返回一個(gè)登錄響應(yīng)。那么這個(gè)流程類似下面這樣
┌────────────────────────────────────────────────────────────┐
│ middleware1 │
│ ┌────────────────────────────────────────────────┐ │
│ │ middleware2 │ │
│ │ ┌──────────────────────────────┐ │ │
│ │ │ middleware3 │ │ │
│ │ │ ┌──────────────────┐ │ │ │
│ │ │ │ │ │ │ │
── Reqeust ─────────┐ │ │ Controller │ │ │ │
│ │ Response │ │ │ │ │ │
<───────────────────┘ │ └──────────────────┘ │ │ │
│ │ │ │ │ │
│ │ └──────────────────────────────┘ │ │
│ │ │ │
│ └────────────────────────────────────────────────┘ │
│ │
└────────────────────────────────────────────────────────────┘
如圖所示請(qǐng)求到達(dá)middleware2后生成了一個(gè)登錄響應(yīng),響應(yīng)從middleware2穿越回中間件1然后返回給客戶端。
中間件接口
中間件必須實(shí)現(xiàn)Webman\MiddlewareInterface
接口。
interface MiddlewareInterface
{
/**
* Process an incoming server request.
*
* Processes an incoming server request in order to produce a response.
* If unable to produce the response itself, it may delegate to the provided
* request handler to do so.
*/
public function process(Request $request, callable $handler): Response;
}
也就是必須實(shí)現(xiàn)process
方法,process
方法必須返回一個(gè)support\Response
對(duì)象,默認(rèn)這個(gè)對(duì)象由$handler($request)
生成(請(qǐng)求將繼續(xù)向洋蔥芯穿越),也可以是response()
json()
xml()
redirect()
等助手函數(shù)生成的響應(yīng)(請(qǐng)求停止繼續(xù)向洋蔥芯穿越)。
中間件中獲取請(qǐng)求及響應(yīng)
在中間件中我們可以獲得請(qǐng)求,也可以獲得執(zhí)行控制器后的響應(yīng),所以中間件內(nèi)部分為三個(gè)部分。
- 請(qǐng)求穿越階段,也就是請(qǐng)求處理前的階段
- 控制器處理請(qǐng)求階段,也就是請(qǐng)求處理階段
- 響應(yīng)穿出階段,也就是請(qǐng)求處理后的階段
三個(gè)階段在中間件里的體現(xiàn)如下
<?php
namespace app\middleware;
use Webman\MiddlewareInterface;
use Webman\Http\Response;
use Webman\Http\Request;
class Test implements MiddlewareInterface
{
public function process(Request $request, callable $handler) : Response
{
echo '這里是請(qǐng)求穿越階段,也就是請(qǐng)求處理前';
$response = $handler($request); // 繼續(xù)向洋蔥芯穿越,直至執(zhí)行控制器得到響應(yīng)
echo '這里是響應(yīng)穿出階段,也就是請(qǐng)求處理后';
return $response;
}
}
示例:身份驗(yàn)證中間件
創(chuàng)建文件app/middleware/AuthCheckTest.php
(如目錄不存在請(qǐng)自行創(chuàng)建) 如下:
<?php
namespace app\middleware;
use ReflectionClass;
use Webman\MiddlewareInterface;
use Webman\Http\Response;
use Webman\Http\Request;
class AuthCheckTest implements MiddlewareInterface
{
public function process(Request $request, callable $handler) : Response
{
if (session('user')) {
// 已經(jīng)登錄,請(qǐng)求繼續(xù)向洋蔥芯穿越
return $handler($request);
}
// 通過(guò)反射獲取控制器哪些方法不需要登錄
$controller = new ReflectionClass($request->controller);
$noNeedLogin = $controller->getDefaultProperties()['noNeedLogin'] ?? [];
// 訪問(wèn)的方法需要登錄
if (!in_array($request->action, $noNeedLogin)) {
// 攔截請(qǐng)求,返回一個(gè)重定向響應(yīng),請(qǐng)求停止向洋蔥芯穿越
return redirect('/user/login');
}
// 不需要登錄,請(qǐng)求繼續(xù)向洋蔥芯穿越
return $handler($request);
}
}
新建控制器 app/controller/UserController.php
<?php
namespace app\controller;
use support\Request;
class UserController
{
/**
* 不需要登錄的方法
*/
protected $noNeedLogin = ['login'];
public function login(Request $request)
{
$request->session()->set('user', ['id' => 10, 'name' => 'webman']);
return json(['code' => 0, 'msg' => 'login ok']);
}
public function info()
{
return json(['code' => 0, 'msg' => 'ok', 'data' => session('user')]);
}
}
注意
$noNeedLogin
里記錄了當(dāng)前控制器不需要登錄就可以訪問(wèn)的方法
在 config/middleware.php
中添加全局中間件如下:
return [
// 全局中間件
'' => [
// ... 這里省略其它中間件
app\middleware\AuthCheckTest::class,
]
];
有了身份驗(yàn)證中間件,我們就可以在控制器層專心的寫業(yè)務(wù)代碼,不用就用戶是否登錄而擔(dān)心。
示例:跨域請(qǐng)求中間件
創(chuàng)建文件app/middleware/AccessControlTest.php
(如目錄不存在請(qǐng)自行創(chuàng)建) 如下:
<?php
namespace app\middleware;
use Webman\MiddlewareInterface;
use Webman\Http\Response;
use Webman\Http\Request;
class AccessControlTest implements MiddlewareInterface
{
public function process(Request $request, callable $handler) : Response
{
// 如果是options請(qǐng)求則返回一個(gè)空響應(yīng),否則繼續(xù)向洋蔥芯穿越,并得到一個(gè)響應(yīng)
$response = $request->method() == 'OPTIONS' ? response('') : $handler($request);
// 給響應(yīng)添加跨域相關(guān)的http頭
$response->withHeaders([
'Access-Control-Allow-Credentials' => 'true',
'Access-Control-Allow-Origin' => $request->header('origin', '*'),
'Access-Control-Allow-Methods' => $request->header('access-control-request-method', '*'),
'Access-Control-Allow-Headers' => $request->header('access-control-request-headers', '*'),
]);
return $response;
}
}
提示
跨域可能會(huì)產(chǎn)生OPTIONS請(qǐng)求,我們不想OPTIONS請(qǐng)求進(jìn)入到控制器,所以我們?yōu)镺PTIONS請(qǐng)求直接返回了一個(gè)空的響應(yīng)(response('')
)實(shí)現(xiàn)請(qǐng)求攔截。
如果你的接口需要設(shè)置路由,請(qǐng)使用Route::any(..)
或者Route::add(['POST', 'OPTIONS'], ..)
設(shè)置。
在 config/middleware.php
中添加全局中間件如下:
return [
// 全局中間件
'' => [
// ... 這里省略其它中間件
app\middleware\AccessControlTest::class,
]
];
注意
如果ajax請(qǐng)求自定義了header頭,需要在中間件里Access-Control-Allow-Headers
字段加入這個(gè)自定義header頭,否則會(huì)報(bào)Request header field XXXX is not allowed by Access-Control-Allow-Headers in preflight response.
說(shuō)明
- 中間件分為全局中間件、應(yīng)用中間件(應(yīng)用中間件僅在多應(yīng)用模式下有效,參見多應(yīng)用)、路由中間件
- 中間件配置文件位置在
config/middleware.php
- 全局中間件配置在key
''
下 - 應(yīng)用中間件配置在具體的應(yīng)用名下,例如
return [
// 全局中間件
'' => [
app\middleware\AuthCheckTest::class,
app\middleware\AccessControlTest::class,
],
// api應(yīng)用中間件(應(yīng)用中間件僅在多應(yīng)用模式下有效)
'api' => [
app\middleware\ApiOnly::class,
]
];
控制器中間件和方法中間件
利用注解,我們可以給某個(gè)控制器或者控制器的某個(gè)方法設(shè)置中間件。
<?php
namespace app\controller;
use app\middleware\Controller1Middleware;
use app\middleware\Controller2Middleware;
use app\middleware\Method1Middleware;
use app\middleware\Method2Middleware;
use support\annotation\Middleware;
use support\Request;
#[Middleware(Controller1Middleware::class, Controller2Middleware::class)]
class IndexController
{
#[Middleware(Method1Middleware::class, Method2Middleware::class)]
public function index(Request $request): string
{
return 'hello';
}
}
路由中間件
我們可以給某個(gè)一個(gè)或某一組路由設(shè)置中間件。
例如在config/route.php
中添加如下配置:
<?php
use support\Request;
use Webman\Route;
Route::any('/admin', [app\admin\controller\IndexController::class, 'index'])->middleware([
app\middleware\MiddlewareA::class,
app\middleware\MiddlewareB::class,
]);
Route::group('/blog', function () {
Route::any('/create', function () {return response('create');});
Route::any('/edit', function () {return response('edit');});
Route::any('/view/{id}', function ($r, $id) {response("view $id");});
})->middleware([
app\middleware\MiddlewareA::class,
app\middleware\MiddlewareB::class,
]);
中間件構(gòu)造函數(shù)傳參
配置文件支持直接實(shí)例化中間件或者匿名函數(shù),這樣可以方便的通過(guò)構(gòu)造函數(shù)向中間件傳參。
例如config/middleware.php
里也可以這樣配置
return [
// 全局中間件
'' => [
new app\middleware\AuthCheckTest($param1, $param2, ...),
function(){
return new app\middleware\AccessControlTest($param1, $param2, ...);
},
],
// api應(yīng)用中間件(應(yīng)用中間件僅在多應(yīng)用模式下有效)
'api' => [
app\middleware\ApiOnly::class,
]
];
同理路由中間件也可以通過(guò)構(gòu)造函數(shù)向中間件傳遞參數(shù),例如config/route.php
里
Route::any('/admin', [app\admin\controller\IndexController::class, 'index'])->middleware([
new app\middleware\MiddlewareA($param1, $param2, ...),
function(){
return new app\middleware\MiddlewareB($param1, $param2, ...);
},
]);
中間件里使用參數(shù)示例
<?php
namespace app\middleware;
use Webman\MiddlewareInterface;
use Webman\Http\Response;
use Webman\Http\Request;
class MiddlewareA implements MiddlewareInterface
{
protected $param1;
protected $param2;
public function __construct($param1, $param2)
{
$this->param1 = $param1;
$this->param2 = $param2;
}
public function process(Request $request, callable $handler) : Response
{
var_dump($this->param1, $this->param2);
return $handler($request);
}
}
中間件執(zhí)行順序
- 中間件執(zhí)行順序?yàn)?code>全局中間件->
應(yīng)用中間件
->控制器中間件
->路由中間件
->方法中間件
。 - 當(dāng)同一個(gè)層次有多個(gè)中間件時(shí),按照同層次中間件實(shí)際配置順序執(zhí)行。
- 404請(qǐng)求默認(rèn)不會(huì)觸發(fā)任何中間件(不過(guò)仍然可以通過(guò)
Route::fallback(function(){})->middleware()
添加中間件)。
路由向中間件傳參(route->setParams)
路由配置 config/route.php
<?php
use support\Request;
use Webman\Route;
Route::any('/test', [app\controller\IndexController::class, 'index'])->setParams(['some_key' =>'some value']);
中間件(假設(shè)為全局中間件)
<?php
namespace app\middleware;
use Webman\MiddlewareInterface;
use Webman\Http\Response;
use Webman\Http\Request;
class Hello implements MiddlewareInterface
{
public function process(Request $request, callable $handler) : Response
{
// 默認(rèn)路由 $request->route 為null,所以需要判斷 $request->route 是否為空
if ($route = $request->route) {
$value = $route->param('some_key');
var_export($value);
}
return $handler($request);
}
}
中間件向控制器傳參
有時(shí)候控制器需要使用中間件里產(chǎn)生的數(shù)據(jù),這時(shí)我們可以通過(guò)給$request
對(duì)象添加屬性的方式向控制器傳參。例如:
中間件
<?php
namespace app\middleware;
use Webman\MiddlewareInterface;
use Webman\Http\Response;
use Webman\Http\Request;
class Hello implements MiddlewareInterface
{
public function process(Request $request, callable $handler) : Response
{
$request->data = 'some value';
return $handler($request);
}
}
控制器:
<?php
namespace app\controller;
use support\Request;
class FooController
{
public function index(Request $request)
{
return response($request->data);
}
}
中間件獲取當(dāng)前請(qǐng)求路由信息
我們可以使用 $request->route
獲取路由對(duì)象,通過(guò)調(diào)用對(duì)應(yīng)的方法獲取相應(yīng)信息。
路由配置
<?php
use support\Request;
use Webman\Route;
Route::any('/user/{uid}', [app\controller\UserController::class, 'view']);
中間件
<?php
namespace app\middleware;
use Webman\MiddlewareInterface;
use Webman\Http\Response;
use Webman\Http\Request;
class Hello implements MiddlewareInterface
{
public function process(Request $request, callable $handler) : Response
{
$route = $request->route;
// 如果請(qǐng)求沒有匹配任何路由(默認(rèn)路由除外),則 $request->route 為 null
// 假設(shè)瀏覽器訪問(wèn)地址 /user/111,則會(huì)打印如下信息
if ($route) {
var_export($route->getPath()); // /user/{uid}
var_export($route->getMethods()); // ['GET', 'POST', 'PUT', 'DELETE', 'PATCH', 'HEAD','OPTIONS']
var_export($route->getName()); // user_view
var_export($route->getMiddleware()); // []
var_export($route->getCallback()); // ['app\\controller\\UserController', 'view']
var_export($route->param()); // ['uid'=>111]
var_export($route->param('uid')); // 111
}
return $handler($request);
}
}
注意
中間件獲取異常
業(yè)務(wù)處理過(guò)程中可能會(huì)產(chǎn)生異常,在中間件里使用 $response->exception()
獲取異常。
路由配置
<?php
use support\Request;
use Webman\Route;
Route::any('/user/{uid}', function (Request $request, $uid) {
throw new \Exception('exception test');
});
中間件:
<?php
namespace app\middleware;
use Webman\MiddlewareInterface;
use Webman\Http\Response;
use Webman\Http\Request;
class Hello implements MiddlewareInterface
{
public function process(Request $request, callable $handler) : Response
{
$response = $handler($request);
$exception = $response->exception();
if ($exception) {
echo $exception->getMessage();
}
return $response;
}
}
超全局中間件
主項(xiàng)目的全局中間件只影響主項(xiàng)目,不會(huì)對(duì)應(yīng)用插件產(chǎn)生影響。有時(shí)候我們想要加一個(gè)影響全局包括所有插件的中間件,則可以使用超全局中間件。
在config/middleware.php
中配置如下:
return [
'@' => [ // 給主項(xiàng)目及所有插件增加全局中間件
app\middleware\MiddlewareGlobl::class,
],
'' => [], // 只給主項(xiàng)目增加全局中間件
];
提示
@
超全局中間件不僅可以在主項(xiàng)目配置,也可以在某個(gè)插件里配置,例如plugin/ai/config/middleware.php
里配置@
超全局中間件,則也會(huì)影響主項(xiàng)目及所有插件。
給某個(gè)插件增加中間件
有時(shí)候我們想給某個(gè)應(yīng)用插件增加一個(gè)中間件,又不想改插件的代碼(因?yàn)樯?jí)會(huì)被覆蓋),這時(shí)候我們可以在主項(xiàng)目中給它配置中間件。
在config/middleware.php
中配置如下:
return [
'plugin.ai' => [], // 給ai插件增加中間件
'plugin.ai.admin' => [], // 給ai插件的admin模塊(plugin\ai\app\admin目錄)增加中間件
];
提示
當(dāng)然也可以在某個(gè)插件中加類似的配置去影響其它插件,例如plugin/foo/config/middleware.php
里加入如上配置,則會(huì)影響ai插件。