Use Laravel's built-in can: middleware to enforce policy authorization checks directly on route declarations.
Instead of calling $this->authorize('update', $post) inside every controller action, you can attach authorization checks directly to route definitions using the can: middleware.
Authorizing Model Actions on Routes
Pass the ability name and the route parameter name to can::
use App\Http\Controllers\PostController;
use Illuminate\Support\Facades\Route;
// Checks PostPolicy::update($user, $post) before invoking the controller
Route::put('/posts/{post}', [PostController::class, 'update'])
->middleware('can:update,post');
// Checks PostPolicy::delete($user, $post)
Route::delete('/posts/{post}', [PostController::class, 'destroy'])
->middleware('can:delete,post');
Authorizing Actions Without Model Instances
When checking abilities that do not require an existing model instance (such as creating new records):
use App\Models\Post;
// Checks PostPolicy::create($user)
Route::post('/posts', [PostController::class, 'store'])
->middleware('can:create,' . Post::class);
Custom Error Handling
If authorization fails, Laravel automatically throws an AuthorizationException (HTTP 403 Forbidden).
Summary
- Enforces authorization at the HTTP routing boundary before controller code runs.
- Resolves implicit route model bindings automatically for policy checks.
- Keeps controller methods focused on request handling and domain logic.
Related Tips
View all tips →Enforce HTTPS URL Generation in Production with URL::forceHttps()
Use URL::forceHttps() in AppServiceProvider to ensure all generated URLs, assets, and pagination links use HTTPS when behind load balancers and reverse proxies.
Secure Actions with Temporary Signed URLs
Generate tamper-proof, time-limited URLs for email verification, invoice downloads, and passwordless logins with URL::temporarySignedRoute().