将策略应用于资源控制器 [英] Apply Policy to Resource Controller
问题描述
我有一个通过Route::resource('User', 'UserController')
定义的CRUD
资源.
I have a CRUD
Resource defined via Route::resource('User', 'UserController')
.
由于可以生成CRUD
登机门和策略,有没有办法应用这种登机门/策略,以便将相应的登机门/策略应用于特定路由?
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?
我认为这将是一种优雅的方式,因为我的警察会匹配我的路线.我正在寻找类似 applyPolicy 的方法或类似的方法:
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.
推荐答案
看看authorizeResource(Model::class)
方法.
在控制器的构造函数中将是一个示例:
An example would be in your controller's constructor:
public function __construct()
{
$this->authorizeResource(Task::class);
}
这篇关于将策略应用于资源控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!