Understanding Laravel Request Lifecycle – How Laravel handles HTTP Requests?
The philosophy behind Laravel application is that, web application trigger routes (i.e URL’s like —-/welcome…). Then, routes invoke controllers, make Views and send responses back to web applications. This post will explain the detailed underlying steps Laravel Framework perform to process Laravel Request Lifecycle.
Whenever, we start learning a new framework, software or tool, the first point comes to our mind is to understand what is the starting point of request, its lifecycle. This helps us to easily grasp the more advanced concepts only if the basic concept is clear. Similarly, first of all, we will learn how the Laravel handles in the incoming HTTP requests – “Laravel Request Lifecycle“.
- Checkout Laravel Tutorial Guide for Beginners
This post is to explain to you how Laravel Request Lifecycle works to serve user requests, all the steps, and stages involved to handle HTTP requests and send back HTTP responses.
Steps of Laravel Request Lifecycle:
- First of all, the entry point of all HTTP requests is public/index.php
- Loads the composer generated the vendor/autoload.php file and get the new Laravel Application Object from bootstrap/app.php
- Then, the request is sent to HTTP Kernels which calls bootstrappers to load configuration, register and call service providers
- HTTP Kernel handles the request and dispatches it to a router.
- Router find the exact route and calls a Controller function
- Controller function validates the input, calls database Models and calls Viewes.
- The HTTP response sent back to incoming users.
Learn Laravel Tutorials:
If you are a beginner to Laravel Framework, then we recommend you to read the following topics:
- Laravel Framework Overview
- Laravel Request Lifecycle
- Top 20 Laravel Artisan Commands
- Understanding Laravel Middleware
- Laravel Routing Tutorial
- Understanding Laravel Blade Template
- Laravel Interview Questions
- Laravel Authentication
- Laravel Validation Tutorial
Let’s understand everything about the Laravel Request Lifecycle:
#STEP1 – Entry point public/index.php
The main entry point for all the HTTP requests to a Laravel application is the public/index.php file. The HTTP web requests are directed to this file by your web server (whether using Apache or Nginx) configuration. The index.php file is just triggering point, doesn’t contain much code. Its is a starting point for loading the rest of the Laravel framework components.
#STEP2 – bootstrap/app.php script
All the HTTP web requests from web applications are directed through the public/index.php script file. When the web application is hosted on Apache web server, then the .htaccess file that ships with Laravel handle the passing of all requests to index.php. After a request enters your index.php file, next the bootstrap/start.php file will be loaded. This file creates the new Laravel Application Object.
Next, the main index.php file loads the Composer generated autoloader definition and then retrieves an instance of the Laravel application from bootstrap/app.php script. The first step performed by Laravel Framework is to create an instance of the application / service container.
This file creates the new Laravel Application object.
#STEP3 -HTTP Console Kernels
The Laravel HTTP kernel layer acts as a central point for serving all the incoming requests from users. After the request processed from the bootstrat/app.php script, it sent to HTTP kernel or console kernel, depending on the type of request from a user. The HTTP kernel file is located at – app/Http/Kernel.php
#STEP4 -Register Service Providers
Next step comes to the Laravel Service Providers. Service providers are responsible for bootstrapping all of the Laravels components, such as the database, queue, validation, and routing components. All of the service providers for the application are configured in the config/app.php configuration file’s providers array. First, the register method will be called on all providers, then, once all providers have been registered, the boot method will be called.
#STEP5 – Dispatch Request to Router
Now, all the service providers are registered, your app/Http/routes.php (routes/web.php in case of Laravel 5.7). Finally, your app/routes.php file will be loaded. When the routes.php file is loaded, the Request object is sent to the application so that it may be dispatched to a route.
Once the application has been bootstrapped and all service providers have been registered, the Request will be handed off 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, in this Laravel Request Lifecycle guide, you have learned all the necessary steps Laravel perform to handle HTTP requests and send back HTTP responses to web applications.
- Checkout Laravel Tutorial Guide for Beginners