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

lorvent's avatar

Authorization and its packages

Hello Guys (back after years)... I have a question.

I want to implement authorization for a project, earlier i have used sentinel, spatie/laravel-permission and other authorization packages

but now when i go to laravel docs, i see default authorization itself it very very good.

Now i have dilemma to use any other package or just default authorization is enough?

Also i prefer having UI to manage all roles ( i can code that)

if possible, please write pros n cons of each in your own experience.

TLDR: is default authorization is well enough or should i use spatie/laravel-permission package?

thank you so much :)

0 likes
9 replies
LaryAI's avatar
LaryAI
AI
Best Answer
Level 58

When it comes to implementing authorization in a Laravel project, both the default authorization system and the spatie/laravel-permission package are excellent choices, each with its own set of pros and cons. Here's a detailed comparison to help you decide which one to use:

Default Laravel Authorization

Pros:

  1. Built-in: No need to install additional packages, reducing dependencies.
  2. Simple and Lightweight: Ideal for straightforward authorization needs.
  3. Gate and Policy System: Provides a flexible way to define authorization logic.
  4. Integrated with Laravel: Seamlessly works with other Laravel features like middleware and request validation.

Cons:

  1. Limited Features: Might not be sufficient for complex role and permission management.
  2. Manual Management: Requires more manual setup and management of roles and permissions.

Example:

// Defining a Gate
Gate::define('update-post', function ($user, $post) {
    return $user->id === $post->user_id;
});

// Using a Gate
if (Gate::allows('update-post', $post)) {
    // The current user can update the post...
}

// Defining a Policy
php artisan make:policy PostPolicy

// In PostPolicy
public function update(User $user, Post $post)
{
    return $user->id === $post->user_id;
}

// Registering the Policy
protected $policies = [
    'App\Models\Post' => 'App\Policies\PostPolicy',
];

// Using a Policy
if ($user->can('update', $post)) {
    // The current user can update the post...
}

Spatie Laravel Permission

Pros:

  1. Feature-Rich: Provides a comprehensive role and permission management system.
  2. Easy to Use: Simplifies the process of assigning roles and permissions.
  3. Flexible: Allows for complex authorization logic and hierarchical roles.
  4. Community Support: Well-documented and widely used in the Laravel community.

Cons:

  1. Additional Dependency: Adds an extra package to your project.
  2. Overhead: Might be overkill for simple authorization needs.

Example:

Conclusion

  • Default Laravel Authorization: Best for projects with simple authorization needs. It is lightweight and integrated into the framework, making it easy to use without adding extra dependencies.
  • Spatie Laravel Permission: Ideal for projects requiring a robust and feature-rich role and permission management system. It simplifies complex authorization logic and is well-supported by the community.

Choose the one that best fits the complexity and requirements of your project. If you anticipate needing advanced role and permission management, spatie/laravel-permission is a great choice. For simpler needs, the default Laravel authorization should suffice.

1 like
lorvent's avatar

@LaryAI thats nice comparision. Basically i want to add a UI aswell and want to limit people which page can be access by them using roles

jlrdw's avatar

@lorvent That's basically what authorization is for, what can or cannot that logged-in user do with their role/s.

Also you still need to strategically use Id's (auth id) in queries so url cannot be spoofed (changed).

1 like
Snapey's avatar

There is nothing in the framework to manage roles and permissions, you need to implement this yourself using the Laravel gates.

IE, the framework contains numerous ways to ask if a user is authorized to do something, but its up to you to implement something that knows how to answer the question. Typically a package like spatie permission allows you to define permissions and roles and assign them to users. Of course you could write this yourself but the package approach can save you a lot of time.

Nothing has changed with authorization since about Laravel 5. Maybe the docs are better?

1 like
puklipo's avatar

Jetstream is an example of a team and role implementation using Laravel's default authorization.

1 like
lorvent's avatar

Thanks everyone for answers :)

i realize not much changed and i should depend on spatie package ....going to use that :)

ARKHAN's avatar

@lorvent I think you can absolutely use Laravel Gates and Policies instead of a roles-and-permissions package. Laravel provides built-in mechanisms to manage access control, which can be as powerful as using a dedicated roles-and-permissions package like spatie/laravel-permission.

lorvent's avatar

@ARKHAN hmm, you are putting me in dilemma now :) did you try that way?

basically i want to build an admin panel with admin role and user role for frontend pages where auth is required, some other roles may be needed for editor etc.

and i really don't need permissions at this moment.

and years back,IIRC i read spatie documentation that we can use their package to extend default gates etc....not really sure if that is the case now.

so depending on my use case..what do you suggest?

ARKHAN's avatar

@lorvent yes you can do that

Laravel's Gates and Policies are lightweight and easy to set up. They allow you to define who can access specific actions (like view, edit, delete) based on roles, without adding the complexity of a full permissions system like Spatie. if later on you need more granular access control (like specific permissions for certain actions), you can extend your existing setup by introducing a package like Spatie's Laravel-Permission.

Use Laravel Gates to define access logic based on roles (e.g., admin, user). Create Policies for actions tied to specific models if needed (e.g., editing an article). Extend later if needed: If permissions become necessary down the line, you can integrate a more robust package like Spatie's Laravel-Permission.

Please or to participate in this conversation.