传递给 Illuminate\Routing\Middleware\ThrottleRequests::addHeaders() 的参数 1 必须是 please sir 的实例
Posted
技术标签:
【中文标题】传递给 Illuminate\\Routing\\Middleware\\ThrottleRequests::addHeaders() 的参数 1 必须是 please sir 的实例【英文标题】:Argument 1 passed to Illuminate\Routing\Middleware\ThrottleRequests::addHeaders() must be an instance of please sir传递给 Illuminate\Routing\Middleware\ThrottleRequests::addHeaders() 的参数 1 必须是 please sir 的实例 【发布时间】:2020-12-19 05:51:26 【问题描述】:已经创建了一个新的中间件来检查用户令牌我已经创建了中间件然后添加到 kernal.php,但是当我尝试在中间件中访问 $request 时出现错误
这是我的中间件代码
请帮忙
public function handle($request, Closure $next, $guard = null)
$token = $request->header('Authorization');
$verify = explode(" ", $token);
if ($verify[0] !== "petani")
return [
'code' => 401,
'error' => 'Token not provided.'
];
if (!$token)
return [
'code' => 400,
'error' => 'Provided token is expired.'
];
try
$credentials = JWT::decode($verify[1], env('JWT_SECRET'), ['HS256']);
catch(ExpiredException $e)
return [
'code' => 400,
'error' => 'Token is expired. '
];
catch(Exception $e)
return [
'code' => 400,
'error' => 'An error while decoding token.'
];
return $next($request);
【问题讨论】:
必须是 please sir 的实例?这是你得到的错误吗? 【参考方案1】:感谢answer:
您应该以这种方式返回响应:
public function handle($request, Closure $next, $guard = null)
$token = $request->header('Authorization');
$verify = explode(" ", $token);
if ($verify[0] !== "petani")
$response= [
'code' => 401,
'error' => 'Token not provided.'
];
return response()->json($response, 401);
if (!$token)
$response= [
'code' => 400,
'error' => 'Provided token is expired.'
];
return response()->json($response, 400);
try
$credentials = JWT::decode($verify[1], env('JWT_SECRET'), ['HS256']);
catch(ExpiredException $e)
$response= [
'code' => 400,
'error' => 'Token is expired. '
];
return response()->json($response, 400);
catch(Exception $e)
$response= [
'code' => 400,
'error' => 'An error while decoding token.'
];
return response()->json($response, 400);
return $next($request);
【讨论】:
猜猜怎么回事,所以会发生这个错误 传递给 Illuminate\Routing\Middleware\ThrottleRequests::addHeaders() 的参数 1 必须是的实例以上是关于传递给 Illuminate\Routing\Middleware\ThrottleRequests::addHeaders() 的参数 1 必须是 please sir 的实例的主要内容,如果未能解决你的问题,请参考以下文章
如何将对象数组作为道具传递给组件,然后将数组的成员作为道具传递给嵌套组件?