Apply Policy to Resource Controller

孤街浪徒 提交于 2020-01-03 08:59:09

问题


I have a CRUD Resource defined via Route::resource('User', 'UserController').

Since it is possible to generate CRUD Gates and Policies, is there a way to apply such a Gate / Policy, so that the corresponding gate / policy is applied to a specific route?

I think that would be an elegant way, since my polices would match my routes. I'm looking for a method like applyPolicy or something simliar:

Route::resource('User', 'UserController')->applyPolicy()

Otherwise I would have to add each policy to each action, which doesn't seem so elegant.


回答1:


Take a look at the authorizeResource(Model::class) method.

An example would be in your controller's constructor:

public function __construct()
{
    $this->authorizeResource(Task::class);
}


来源:https://stackoverflow.com/questions/44501430/apply-policy-to-resource-controller

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!