2017-08-01 102 views
2

我想在laravel form請求授權裏面訪問路由請求參數。我無法找到一個描述這一點的例子。如何在表單請求中訪問請求的非id屬性?

// Works fine when you want id 
dd($this->route('myResourceName')); 

// How to do when I want something else??? 
dd($this->route('anotherAttribute')); 
// Above give null probably because it is a resourceful controller 

在一個側面說明,我不明白這個設計,有什麼意義? $ this-> route('anyAttribute')會是最簡單的,對吧?

編輯:更廣泛的例子

class UpdateSlotAPIRequest extends APIRequest 
{ 
    public function __construct(){ 
     parent::__construct();   
     $this->slot = Slot::find($this->route('slot')); 
     $this->access_token = $this->route('access_token'); // this is not working! 
    }  
    /** 
    * Determine if the user is authorized to make this request. 
    * 
    * @return bool 
    */ 
    public function authorize() 
    { 
     // If administrator is logged in all is good. 
     // If slot is free its ok. 
     // If its not free but you provide good access_token its also fine. 
     return Auth::check() || $this->slot->isAvailable() || $this->slot->isValidAccessToken($this->access_token); 
    } 
... 

```

+0

發現它是什麼'任意請求parameters'是什麼意思?你的意思是路由參數? –

+0

只更正路線參數! – Anders

+0

你可以添加你如何使用它的例子。你是從控制器還是中間件調用它? –

回答

1
$access_token = request()->input('access_token'); 

https://laravel.com/docs/5.4/helpers

+0

希望這是一個很好的教訓。如果您有任何問題,請嘗試使您的第一個搜索文檔的點,然後... **搜索** stackoverflow,然後如果沒有解決方案,即使在谷歌搜索*然後問問題... –

+0

該解決方案需要一個全球幫手,我懷疑這不是最好的解決方案。表單請求的文檔不包括我的用例,因此沒有相關的結果結果。 – Anders

+0

所以你的意思是你不能只在FormRequest類中說'$ this-> get('access_token')'?我以爲'this'也是基類Request類的擴展? –