An attempt to illustrate the laravel request lifecycle that is documented here (version 12.x when this was uploaded): https://laravel.com/docs/master/lifecycle
TLDR file is the tldraw.com source file.
Feel free to reuse as you'd like and please share any improvements. If you find any issues or comments please let me know.
The Laravel request lifecycle follows a the following path from the initial HTTP request to the final response:
All requests to a Laravel application begin at the public/index.php file. This file:
- Sets up the application environment
- Loads Composer's autoloader
- Retrieves the application instance from
bootstrap/app.php
The request is then passed to either:
- HTTP Kernel (
App\Http\Kernel) for web requests - Console Kernel for CLI commands
- The console kernel has it's own middleware
The HTTP Kernel loads several bootstrap classes that:
- Configure error handling
- Configure logging
- Detect the environment
- Register service providers
- These include any packages or user provided services listed under bootstrap/providers.php
- Boot service providers
Before the request reaches the application logic, it passes through global middleware such as:
- Checking for maintenance mode
- Verifying CSRF tokens
- Setting common headers
- Managing sessions
- If you want a middleware to run during every HTTP request to your application, you may append it to the global middleware stack in the application's bootstrap/app.php file
- Then it proceeds to run other middleware
The router:
- Matches the request URL against defined routes
- Loads and executes any route-specific middleware
- Resolves the controller or closure to handle the request
The controller:
- May inject dependencies from the service container
- Processes the request data
- Interacts with models and services
- Prepares the response data
The application:
- Formats the response (HTML, JSON, etc.)
- Sets appropriate headers
- Attaches cookies if needed
The response passes through middleware again, but in reverse order, allowing for:
- Response modification
- Additional headers
- Final processing
Finally, the response is sent to the client's browser.
After sending the response, Laravel runs termination callbacks and cleanup tasks including any termination middleware (on supported servers).