gpt4 book ai didi

php - 传递给 Illuminate\Routing\Middleware\ThrottleRequests::addHeaders() 的参数 1 必须是

转载 作者:行者123 更新时间:2023-12-04 02:39:41 25 4
gpt4 key购买 nike

我创建了一个新的中间件来检查用户 token 我创建了中间件然后添加到 kernal.php,但是当我尝试访问中间件中的 $request 时出现错误

这是我的中间件代码

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);
}

关于php - 传递给 Illuminate\Routing\Middleware\ThrottleRequests::addHeaders() 的参数 1 必须是,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59971698/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com