2016-05-19 32 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奇怪的行爲不起作用。希望有人能澄清。