Mastering Authorization: Gates and Policies in Laravel and Beyond with Elisha
Автор: PHP Talks
Загружено: 2026-01-25
Просмотров: 1
Welcome to PHPTalks Sessions!
In this session, we dive deep into the essential concepts of authorization, focusing on Gates and Policies, particularly within the Laravel framework, but also how these concepts extend to other PHP projects like Symfony and vanilla PHP.
The talk is led by @elishaukpong637 who covers:
1. Authentication vs. Authorization
Authentication: Declares who you are, giving first-level access to the platform (e.g., logging in with a username/email and password).
Authorization (The Focus): Decides what you can do with that access, determining to what extent you have certain privileges and preventing unauthorized access to specific actions or data.
Security Risk: Ignoring authorization can lead to huge security breaches, such as User A accessing or editing User B's data.
Design Decision: Authorization should be a design decision from the get-go, not an afterthought.
2. Implementing Authorization in Laravel
Laravel provides two patterns for implementing authorization checks:
Gates: These are closure-based authorization checks and are typically used for standalone actions that are not tied to a specific model (resource).
Example: Checking access to an admin dashboard.
Gates are defined in a service provider, such as the AppServiceProvider.
Policies: These are class-based authorization checks and are used for actions that tie directly to a particular model (resource).
Example: Implementing authorization for a Post model, mapping to actions like create, update, read, and delete.
Policies are created using PHP artisan make:policy [PolicyName] --model=[ModelName].
3. Benefits of Using Gates and Policies
Security: Prevents unauthorized users from seeing or taking actions they shouldn't.
Centralized Rules: Keeps your controllers thin by centralizing authorization logic, making it easy to reuse checks across your application.
4. Practical Demo
Elisha walks through a live demo using a Post and Comment model to show how policies are defined and used to restrict actions such as viewing, updating, and deleting records based on the user's ID and the resource's status (e.g., whether a post is published).
The practical examples demonstrate how to use this-authorize() within controllers and form requests.
She highlights how an unauthorized action results in a 403 Forbidden status code.
Connect with the speaker and the PHPTalks community for further discussion and access to the codebase!
Connect with our Speaker
X: https://x.com/drumzminister
For more updates and sessions visit
@PHPTalks
Our X
https://x.com/PhpTalks
Доступные форматы для скачивания:
Скачать видео mp4
-
Информация по загрузке: