Authentication vs. authorization
Authentication verifies “who” a user is. Signing in is the canonical example. Authorization decides “what” that user is allowed to do. For example, you might let users edit only their own posts, or restrict configuration changes to administrators. Once you’ve implemented sign-in through authentication, authorization is your next step.Laravel’s authorization features offer two approaches: gates and policies. Which to use depends on your use case.
Gates
Gates are simple closure-based authorization checks. They’re well suited to permission checks that aren’t tied to a particular model.Authorization flow with gates
Defining a gate
Define gates in theboot method of App\Providers\AppServiceProvider using Gate::define().
Checking permissions with a gate
To check permissions in a controller, useGate::allows() or Gate::denies().
Throwing exceptions with Gate::authorize()
UseGate::authorize() to automatically return a 403 response when the user is unauthorized. You can skip the manual abort(403).
Administrator bypass (the before method)
To grant administrators every permission, useGate::before().
before closure returns anything other than null, that result becomes the final permission decision. Returning null (or nothing) falls through to the normal gate check.
@can / @cannot in Blade templates
The@can and @cannot directives make it easy to run gate checks in your templates.
Policies
A policy is a class that groups authorization logic related to a particular model. For managing create, view, update, and delete permissions on aPost model, a policy is a better fit than a gate.
Authorization flow with a policy
Generating a policy
Use themake:policy Artisan command to generate a policy class.
--model option to scaffold a policy that includes every CRUD method for that model.
app/Policies/PostPolicy.php is generated.
Model and policy auto-discovery
By default, Laravel automatically discovers policies based on a naming convention.- Model:
app/Models/Post.php - Policy:
app/Policies/PostPolicy.php
If you deviate from the naming convention or want to register a policy manually, use In Laravel 13, you can also register a policy declaratively by attaching the
Gate::policy() in the boot method of AppServiceProvider.#[UsePolicy] attribute to the model.Implementing policy methods
A policy generated with the--model option includes methods that correspond to the standard CRUD actions.
Administrator bypass (the before method)
Policies also support abefore method that grants administrators every permission.
Order of before callbacks
Using policies from controllers
The authorize() method
Laravel controllers include anauthorize() helper method (when you’re not extending the base controller, use Gate::authorize() instead).
- User::can() / cannot()
Registering RESTful policies in bulk with authorizeResource()
CallingauthorizeResource() in the constructor automatically wires each controller action to the corresponding policy method.
Authorization via middleware
To perform authorization at the route level, use thecan middleware.
can method:
Using policies in Blade templates
Once a policy is registered, Blade’s@can / @cannot directives automatically use it.
Practical example: managing blog post permissions
Here’s how to implement “only the author can edit or delete their own posts” in a blog application.1
Generate the policy
2
Implement the policy methods
3
Add authorizeResource() to the controller
4
Add permission checks to Blade templates
Summary
Choosing between gates and policies
Choosing between gates and policies
- Gates: simple permission checks that aren’t tied to a model — such as access to an admin dashboard or permission to change global settings.
- Policies: managing CRUD permissions for a model. Create a policy class for each resource such as
Post,Order, orComment.
Common APIs
Common APIs
Common Artisan commands
Common Artisan commands