[英]How can i manage controller methods as per user role in laravel?
public function __construct()
{
$this->middleware('roles:Author')->only(['index','show','create']);
$this->middleware('roles:User')->only(['index','show']);
}
In my controller i want access methods as per users role for eg if user role is admin then he has to access all methods of controller, if user role is Author then he has access of index,create and show method and if role is User then he has only access of index and show method.在我的控制器中,我希望根据用户角色访问方法,例如,如果用户角色是管理员,则他必须访问控制器的所有方法,如果用户角色是作者,则他可以访问索引、创建和显示方法,如果角色是用户,则他只能访问 index 和 show 方法。
You can take a look at Gates
( docs ).你可以看看
Gates
(文档)。
In your App\\Providers\\AuthServiceProvider
add:在您的
App\\Providers\\AuthServiceProvider
添加:
Gate::define('create-post', function ($user) {
return $user->isAuthor(); //Here you should check the users role
});
And then in your controller's method create()
:然后在您的控制器方法
create()
:
if (Gate::allows('create-post')) {
// The current user can create posts...
}
The other two methods: index()
and show()
are available to both roles so there's no action required.另外两个方法:
index()
和show()
对这两个角色都可用,因此不需要任何操作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.