我的routes.php
文件中有一个路由组,中间件指定如下:
Route::group(['prefix' => 'api', 'middleware' => 'api'], function() {
Route::post('oauth/access_token', function() {
return Response::json(Authorizer::issueAccessToken());
});
}
我正在使用Lucadegasperi Oauth2 服务器插件。$middleware
对于它的设置,我必须在文件数组Kernel.php
(内核类)中输入以下 LucaDegasperi 项:
protected $middleware = [
\Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class,
\LucaDegasperi\OAuth2Server\Middleware\OAuthExceptionHandlerMiddleware::class,
];
同一个类的$middlewareGroups
数组如下:
protected $middlewareGroups = [
'web' => [
\App\Http\Middleware\EncryptCookies::class,
\Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
\Illuminate\Session\Middleware\StartSession::class,
\Illuminate\View\Middleware\ShareErrorsFromSession::class,
\App\Http\Middleware\VerifyCsrfToken::class,
],
'api' => [
'throttle:60,1',
],
];
它的OAuthExceptionHandlerMiddleware
作用是格式化 JSON 响应的异常。现在,当我'middleware' => 'api'
如图所示在路由组中应用时,全局中间件不起作用。我可以这样说是因为 HTML 错误页面会在异常发生时显示。但是,当我在路由组中省略 时'middleware' => 'api'
,全局中间件会起作用,并且我会收到错误的 JSON 响应。
我该如何度过这个难关?