如何在表单请求中访问请求的非id属性?

时间:2017-08-01 17:30:54

标签: laravel

我想在laravel表单请求授权内访问路由请求参数。我找不到一个描述这个的例子。

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

```

1 个答案:

答案 0 :(得分:1)

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

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

中找到它