Laravel Request Lifecycle
This blog is to help you get to know about Laravel’s ‘Request Life Cycle‘ i.e, how this framework processes the given request in different stages and provide the response to the user. We will look into this as step by step process for better understanding.
The entry point for all requests to a Laravel application is the
public/index.php file. All requests are directed to this file by your web server (Apache / Nginx) configuration. The
index.php file doesn't contain much code. Rather, it is a starting point for loading the rest of the framework.
It loads the auto loader files which is generated by composer.
Then it retrieves an instance of the Laravel application from
bootstrap/app.php script. Laravel itself creates an instance of the application, is the initial/first step.
Next step will occur on the Kernel part of the application.
The incoming request is sent to either the HTTP kernel or the console kernel, depending on the type of request that is entering the application .These two kernels serve as the central location that all requests flow through.
HTTP kernel, which is placed in
app/Http/Kernel.php. It just receive a Request and return a Response. Bootstrappers that are defined by the Kernel class, which configures error handling, configure logging, detect environments and other tasks to be done before the request handled.
HTTP Kernel will define the list of middleware that are passed through before handled by application.
Next step of the kernel is to load service providers as part of the bootstrapping action. Providers that are needed for the application are placed in
config/app.php configuration file.
While the register method calls, all the providers will be registered. Once all providers are registered, then boot method will be called.
Once the application have been bootstrapped and all service providers are registered and booted, the request will be handed over to the router for dispatching. The router will dispatch the request to a route or controller, as well as run any route specific middleware.
Now request will be dispatched by the Router and it will end up with the views as shown below:
Router will direct the HTTP Request to a Controller or return a view or responses directly by omitting the controller. These routes will be placed in
app/controllers/ performs specific actions and sends data to a View.
app/views/ formats the data appropriately, providing the HTTP Response.
The above steps are clearly explained in the diagrammatical view above.