本文介绍了传递给Illuminate \ Routing \ Middleware \ ThrottleRequests :: addHeaders()的参数1必须是的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我创建了一个用于检查用户令牌的新中间件,然后创建了中间件,然后将其添加到kernal.php中,但是当我尝试访问中间件中的$ request时,我得到了错误
I have Created a new middleware for checking the user token I have create middleware then adeded to kernal.php and but when i tried to access $request in middleware i am getting the error
这是我的中间件代码
namespace App\Http\Middleware;
use Illuminate\Http\Request;
use Closure;
use App\User;
class CheckToken
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
if($request->header('Token') == '123')
{
return ['status' =>2, 'msg' => 'Unathorized'];
}
else
{
return $next($request);
}
}
}
这是我的核心文件
protected $routeMiddleware = [
'auth' => \App\Http\Middleware\Authenticate::class,
'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
'bindings' => \Illuminate\Routing\Middleware\SubstituteBindings::class,
'cache.headers' => \Illuminate\Http\Middleware\SetCacheHeaders::class,
'can' => \Illuminate\Auth\Middleware\Authorize::class,
'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
'signed' => \Illuminate\Routing\Middleware\ValidateSignature::class,
'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
'verified' => \Illuminate\Auth\Middleware\EnsureEmailIsVerified::class,
'verifyToken' => \App\Http\Middleware\CheckToken::class,
];
我收到此错误
Argument 1 passed to Illuminate\Routing\Middleware\ThrottleRequests::addHeaders() must be an instance of Symfony\Component\HttpFoundation\Response, instance of Illuminate\Http\Request given, called in C:\xampp\htdocs\idoltime\vendor\laravel\framework\src\Illuminate\Routing\Middleware\ThrottleRequests.php on line 62
推荐答案
您可以使用
// return $request;
if($request->header('Token') == '123')
{
$response = [
'status' => 2,
'message' => 'Unauthorized',
];
return response()->json($response, 413);
}
else
{
return $next($request);
}
这篇关于传递给Illuminate \ Routing \ Middleware \ ThrottleRequests :: addHeaders()的参数1必须是的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!