Gates are similar to policies but more simple. It's all broken down here:
https://laravel.com/docs/12.x/authorization
I myself use laravel authentication but use custom RBAC with it.
Hello,
With Laravel, it's possible to manage authorizations with gates and/or policies.
But what is the real difference between a gate and a policy ?
Here is an example of how I use both.
Can you simply tell me what you think about it ?
I have defined roles and each role has some permissions. Then it's possible to assign roles to users and eventually permissions directly to users, but it's better to assign only roles to keep always the same logic.
To define all permissions, I'm using gates with Gate::define(). So I can for example define a gate to check if a user is authorized to delete a task.
Gate::define('delete-task', function (User $user) { ... });
And for handling authorizations according to the business logic itself, for example a task can be deleted only if it has been completed, I'm using the policies.
public function delete(User $user, Task $task)
{
return $task->is_completed === true;
}
But I also want that the user is authorized to trigger the action of deleting a task.
public function delete(User $user, Task $task)
{
return $user->can('delete-task') && $task->is_completed === true;
}
Thanks for sharing how you are using gates and policies.
V
I think of it like
Gate = can you do something
Policy = can you do something with a specific object
Policy can use gates to simplify the policy.
Please or to participate in this conversation.