0
我在中间件的一些数据存储如下:Laravel,在自定义请求类数据丢失
public function handle($request, Closure $next)
{
//checking above..
if($this->authed()){
$request->token= $token; //here store the token in user request
return $next($request);
}
return response('Invalid credentials', 401);
}
然后在控制器我调用自定义的请求类
//function in controller
public function upload(CoverageValueRequest $request){ //calling a custom request
dd($request->token);
dd($request->all());
}
第一DD会给空。但是,如果我将CoverageValueRequest
更改为Request
,它将打印该令牌。
P.S:使用Content-Type:application/json将来自REST调用的$ request数据作为JSON格式。我不确定这是否相关。
更新
我已经改变了在中间件以下仍然相同的行为:
$request->merge(['token' => $token]);
不能如果CoverageValueRequest
获得令牌,但如果它是Request
然后它工作。
public function handle($request, Closure $next)
{
//checking above..
if($this->authed()){
$request->attributes->add(['token' => $token]);
return $next($request);
}
return response('Invalid credentials', 401);
}
然后在控制器中检索如下: