Laravel request lifecycle overview
public/index.php: The entry point to your Laravel apps
The entry point for all requests to a Laravel application is the public/index.php
file. Your web server (e.g., Nginx/Apache) will direct all web traffic to this file.
Here’s what that file looks like at the time of writing (August, 2024):
<?php
use Illuminate\Http\Request;
define('LARAVEL_START', microtime(true));
// Determine if the application is in maintenance mode...
if (file_exists($maintenance = __DIR__.'/../storage/framework/maintenance.php')) {
require $maintenance;
}
// Register the Composer autoloader...
require __DIR__.'/../vendor/autoload.php';
// Bootstrap Laravel and handle the request...
(require_once __DIR__.'/../bootstrap/app.php')
->handleRequest(Request::capture());
It does the following:
- Checks if the app is in maintenance mode and if so, requires a “maintenance” file that’ll display a maintenance page on the UI.
- Registers the Composer autoloader.
- Retrieves an instance of the Laravel app and calls its
handleRequest()
method.
The request is sent to either the HTTP kernel or the console kernel depending on the type of request.
The HTTP kernel does a number of things:
- Defines an array of
bootstrappers
that will be run before the request is executed. These bootstrappers configure error handling, configure logging, detect the application environment, and perform other tasks that need to be done before the request is actually handled. Typically, these classes handle internal Laravel configuration that you do not need to worry about. - Passes the request through the middleware stack. Middleware handles reading & writing the HTTP session, verifying the CSRF token, checking if the app is in maintenance mode, etc.
Think of the HTTP kernel as a black box that receives a Request
and returns a Response
.
Service providers: How Laravel provides its main features
One of the key kernel bootstrapping actions is loading the service providers for your application. The service providers bootstrap all of the framework’s various components like the database, queue, validation, and routing components.
Laravel will iterate through the list of providers, instantiate them, and call their register
methods.
Once all the providers have been registered, their boot
method will be called. Laravel registers all of them first before re-iterating through them to call their register
methods to ensure the service providers can depend on every container binding being registered & available by the time their boot
method is executed.
The framework provides all its major features through service providers. You can also create your own to add more functionality.
You can find the list of user-defined or third-party service providers in your application via the bootstrap/providers.php
file.
Routing
Once the app has been bootstrapped and all the service providers have been registered, the Request
will be handed to the router which will dispatch the request to a route/controller as well as execute any route-specific middleware.
Request middleware allow you to examine an incoming HTTP request and take actions as needed (e.g., prevent the request going through to a controller if the user is unauthenticated or the app is in maintenance mode).
If all the incoming middleware lets the request proceed, the controller will return a response and the response will travel back outward through any response middleware. Response middleware gives you the chance to modify the response as needed.
If the response also passes through the route middleware, the HTTP kernel’s handle
method returns that response object to the handleRequest
method of the application instance which in turn calls the send
method on the returned response.
The send
method sends the response content to the user’s web browser. And that’s the Laravel request lifecycle!