Laravel Request Lifecycle
Lets dive into the magic of Laravel and the way it handles http requests. First of all, the entry point for all the requests is
the public/index.php
file. It is usually called the front controller. The web server (Apache or Nginx) send all requests
to this file. So, it may be considered as the starting point for loading the application.
First of all it loads the Composer generated autoloader file vendor/autoload.php
and retrieves and instance Laravel
application (Illuminate\Foundation\Application
) from the bootstrap/app.php
file.
This instance serves as the “glue” for all
the components of Laravel, and is the IoC container itself. There are some bindings to the IoC container in the bootstrap/app.php
file: for Http Kernel, Console Kernel and Exception Handler:
Kernel
Next, request is served by the HTTP kernel or cli kernel, it depends on the type of the request. It this article I will focus on
HTTP requests and HTTP kernel, that handles them. It is located in app/Http/Kernel.php
:
Http kernel extends the Illuminate\Foundation\Http\Kernel. This class has an array of bootstrapers that will be run befoure the request is executed. They detect environment and load configuration, configure logs and error handling. They also register providers with facades, and then boot providers:
The http kernel also has a list of HTTP middlewares (global and for specific routes). Every request passes through all the global middlewares before being handled by the kernel.
The process of handling a request is very simple. Kernel has handle()
method, that recieves a Request
object and returns
a Response
object:
I will not dive deeper in the process of how the router works. I treat kernel as a black box that represents my application.
Service Providers
The most important Kernel bootstrapper is one that registeres service providers (Illuminate\Foundation\Bootstrap\RegisterFacades
).
It calls registerConfiguredProviders()
method of the application instance:
The list of service providers for the application is configured in the config.app
file, in providers
array. First, providers are registered and
on every provider the register
method will be called. Then, when all the providers have been registered, they must be booted by another bootstrapper
(Illuminate\Foundation\Bootstrap\BootProviders
). It simply calls boot
method of the application instance, which calls boot
method of every
service provider:
Service providers are responsible for bootstraping all of the framework’s and application components. For example, validation, database, routing and so on. They bootstrap and configure every feature of the framework or application. Service providers are the most important part of the entire application bootstrap process.
Dispatch Request
After bootstrapping the application and registering service providers, the Request will be dispatched by the router:
And then the router will dispatch the request to a router or controller:
Conclusion
Steps of the request lifecycle:
- The Request is send to
public/index.php
. bootstrap/app.php
loads Composer autoloader and creates an instance of the application and binds kernels with the exception handler.- Kernel calls bootstrappers, that loads configuration, detect environment, register and then boot service providers.
- Kernel handles the request and dispatch it to the router.
- Router calls before filter.
- Router finds the matched route and calls the route before filters.
- Route calls its action.
- Router calls route after filters.
- Router calls app after filters.
- The middleware stack cascades the Response back up the chain
- The Response is sent to the user.