and if I were to define another gate (using a different name) that points to the same method on policy class, I can use both gates....does this mean that I have two gates that point to the same thing.....
one confusing thing about Gates and Policies
So I was reading the laravel authorization part docs. I am really confused about how to write gates.
I understand that I can define a Gate either using closures or Class@method style callback string, like controllers. and that is going to call the policy method. and then we call Gate::allows() or Gate::()denies() to check.
However, if I only create a policy class and not to define a Gate in the service provider. I actually still can call
Gate::allows() and Gate::()denies() and this still works.
Does this mean Gate::define() is redundant if I only write gates in Class@method way? or did I missing something in the doc.
Thanks
Every method inside a Policy should have its own name (its a php class).
You are not limited in the names
As an example I have a model called Action and in ActionPolicy I have
class ActionPolicy
{
use HandlesAuthorization;
public function create(User $user) { ... }
public function beOwner(User $user, Action $action) { ... }
}
then later in the route/web.php
I use them via the middleware option
Route::middleware('can:create,App\Action')->group(function() { ... });
Route::middleware('can:beOwner,action')->group(function () {
Route::get('action/{action}/edit', 'ActionController@edit')->name('owner.edit');
}
Please or to participate in this conversation.