期望通過(guò)一個(gè)全局中間件在響應(yīng)的header里添加服務(wù)節(jié)點(diǎn)信息,但某些情況下并未執(zhí)行全局中間件。
項(xiàng)目相關(guān)文件如下:
<?php
namespace app\middleware;
use Webman\MiddlewareInterface;
use Webman\Http\Response;
use Webman\Http\Request;
class AllinMiddleware implements MiddlewareInterface
{
public function process(Request $request, callable $handler) : Response
{
$response = $handler($request);
$response->withHeaders([
'Via' => config('app.name', 'ziyo.bot') . '/' . config('app.version', '1.0.0'),
]);
return $response;
}
}
<?php
return [
'' => [
app\middleware\AllinMiddleware::class,
]
];
<?php
use Webman\Route;
use Webman\Http\Response;
// 被定義的路由會(huì)執(zhí)行全局中間件
Route::get('/', function () {
return 'Hello Webman!';
});
// 這里的請(qǐng)求未執(zhí)行中間件
Route::fallback(function(){
$data = ['code' => 404, 'msg' => '404 not found'];
$options = JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES;
return new Response(404, ['Content-Type' => 'application/json'], json_encode($data, $options));
});
Route::disableDefaultRoute();
Route::fallback 是否可以指定一下中間件,或執(zhí)行全局中間件