Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

AbdelrahmanFathy's avatar

Laravel Gate & Blade directive

In Laravel 11, i created a new Service Provider class for authorization, and defined gates and blade directives in the boot method like these:

    Gate::define('admin', function (User $user) {
        return $user?->type === 0;
    });

    Gate::define('author', function (User $user) {
        return $user?->type === 1;
    });

    Blade::if('admin', function () {
        return request()->user()?->can('admin');
    });

    Blade::if('author', function () {
        return request()->user()?->can('author');
    });

My question: Is defining these gates and blade directives in the boot method is right or not a good practice to define much logic in the boot method ? and if yes, where can i defining them ? Thanks in advance

0 likes
1 reply
puklipo's avatar

boot is the right.

If there are too many definitions, you can separate the methods or service providers.

public function boot(): void
{
     $this->gate();
     $this->blade();
}

private function gate(): void
{
}

private function blade(): void
{
}

Please or to participate in this conversation.