Laravel Flashcards
What are middlewares in Laravel?
Middleware in Laravel is the program that executes before a controller responds to a request. It is a method of preventing requests from reaching the controller and processing them instead. Logging, caching, authentication, and permission are just a few applications for this.
php artisan make:middleware Authenticate
Global middleware
Route middleware
Controller middleware
Terminable middleware
What is Laravel Passport?
Is an OAuth server implementation, and is used to offer OAuth authorization for your application.
What is Laravel Sanctum?
Sanctum is an authentication library for “simpler” token-based authentication for clients that need it (i.e. mobile apps) but also offers cookie-based authentication for SPAs.
What is a Laravel guard?
Guards in Laravel define how users are authenticated in your application. They determine where the application should look for user credentials and how to verify them. Laravel provides multiple guard drivers out of the box, such as “web” (for browser sessions) and “api” (for API token authentication).
What is a Laravel Gate?
Gates are used for defining and checking authorization policies in Laravel. They allow you to define fine-grained access control rules for different actions or operations in your application. Gates are often used within your code to check if a user is authorized to perform a specific action.
What is a Laravel Policy?
Policies are similar to gates but provide a more organized way to manage authorization logic for specific models in your application. Each model can have its own policy class that defines the authorization rules for that model’s actions (e.g., creating, updating, deleting).
What are the key differences between guards, gates and policies?
Guards: Guards handle user authentication and define where the application should look for user credentials (e.g., sessions, tokens).
Gates: Gates are used to define and check authorization policies for specific actions or operations in your application.
Policies: Policies are used to define authorization policies for specific models, making it easier to manage rules for CRUD operations related to those models.
Request Lifecycle
1 - index.php -> autoloading and creating the Laravel app instance
2 - Kernel -> Manage Req and Res objects. Also Middlewares
3 - Service Providers -> Dependency Injection
4 - Routing -> match route and send to controller
5 - Controller -> handle request and process data
6 - Model -> if controller needs
7 - Response -> after processing the request, the controller prepares a response.
8 - Middleware Again ->
9 - Send response