2016-05-19 33 views
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); 
} 

然后在控制器中检索如下:

回答

0

我已经通过设置令牌属性要求,中间件类中找到了答案

//function in controller 
public function upload(CoverageValueRequest $request){ //calling a custom request 
    dd($request->attributes->get('token')); 
    dd($request->all()); 
} 

我不知道为什么直接分配或merge奇怪的行为不起作用。希望有人能澄清。