Gates - AuthServiceProvider - Performance I am new on learning gates and policies and prefer not to use a plugin for roles and permissions
Will it be an issue(Performance wise) if i have around 100 Gates defined in the AuthServiceProvider.
100 individual Gates will not be so significant compared to loading the framework, handling the request, fetching data, returning a response etc etc..
How you define the Gates may be inefficient depending on where you store state, and how you retrieve that state, e.g. 100 individual database queries would be quite inefficient.
@zaster The number of gates won’t really matter. Gates are only invoked when you call them, i.e. Gate::allows('gate-name').
However, if you’re working with policies then I’d recommend corresponding policy class over defining individual gates.
@martinbean
Aren't those gates get loaded when the boot function is executed?
<?php
namespace App\Providers;
use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;
use Illuminate\Support\Facades\Gate;
class AuthServiceProvider extends ServiceProvider
{
/**
* The policy mappings for the application.
*
* @var array
*/
protected $policies = [
// 'App\Model' => 'App\Policies\ModelPolicy',
];
/**
* Register any authentication / authorization services.
*
* @return void
*/
public function boot()
{
$this->registerPolicies();
Gate::define('publish-invoices', function ($user) {
return $user->is_admin;
});
Gate::define('manage-invoices', function ($user) {
return $user->is_admin;
});
Gate::define('view-invoices', function ($user) {
return $user->is_admin;
});
}
}
@zaster It registers the callbacks, but the callbacks won’t actually get invoked until you call a gate using something like Gate::allows.
Please sign in or create an account to participate in this conversation.