Laravel 5.4中间件问题

时间:2017-07-12 10:53:36

标签: php laravel-5.4

当我尝试通过中间件路由时,它会给我内部服务器错误。状态500。 我似乎不明白是什么导致了这种行为。

贝娄是我的代码

kernel.php

protected $routeMiddleware = [
    'jwt.auth' => \App\Http\Middleware\VerifyJWTToken::class,

];

web.php

Route::group(['middleware' => 'jwt.auth'], function () 
{
    Route::get('guser', 'UserController@getUser');

});

VerifyJWTToken

 class VerifyJWTToken
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
       /* try
        {
            $user = JWTAuth::toUser($request->input('token'));
        }
        catch (JWTException $e) 
        {
            if($e instanceof \Tymon\JWTAuth\Exceptions\TokenExpiredException) 
            {
                return response()->json(['token_expired'], $e->getStatusCode());
            }else if ($e instanceof \Tymon\JWTAuth\Exceptions\TokenInvalidException) 
            {
                return response()->json(['token_invalid'], $e->getStatusCode());
            }else
            {
                return response()->json(['error'=>'Token is required']);
            }
        }*/
       return $next($request);
    }
}

1 个答案:

答案 0 :(得分:0)

您必须在路由::群组中传递中间件数组而不是字符串
只需查看5.4/middleware#registering-middleware

即可

这样做

Route::group(['middleware' => ['jwt.auth']], function () 
{
    Route::get('guser', 'UserController@getUser');

});