Request life cycle of Laravel (How laravel works under the hoods)
Hey there! I have been developing a laravel based application for more than a year now. I know a thing or two about how to write code on Laravel but truth to be told; I know very less about how Laravel handles all the executions under the hood. Recently I & some of my colleagues have decided to learn how laravel works its magic. So as a part of that, I studied “Laravel Request Lifecycle” today. And I am going to share what I learned from it. Yes, I know you can read about that on laravel documentation, but in this blog, I am going to break it down step by step.
But first, let me give you a quick example of why you should learn about that. Take riding a motorcycle, for example. It’s not mandatory to learn how the motorcycle engine works, simply learning how to ride a bike is enough. But when you know about its engine, how it works, what kind of oil it requires, it’s capabilities, then you become more confident while riding it. Because you know everything about and that gives you more confidence utilizing its full potential. Learning what happens under the hood of the Laravel framework is almost same. In my opinion, It’s essential to understand the inner workings of any tool you are given to work with, a complete understanding of that tool will enable you to use all the power that comes with the tool.
The entry point (Eg: https://https://shoroborno.com or any other URL) 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. If you are familiar with Laravel, I think you have seen the file. It doesn’t have much code in it. It’s simply an entry point which loads all the necessary files to handle the request you submitted from your web browser.
HTTP / Console Kernels
The next step occurs on the Kernel part of the application. There are two kinds of Kernel request HTTP or Console.
The request is sent to either HTTP or Console Kernels based on the request which has been sent to the application. These two Kernels acts as a central location that all requests flow through. For this blog, let’s focus on the HTTP part of Kernels which handles the requests sent to your application via HTTP method which is located in app/Http/Kernel.php .
It receives a request and returns a response. All of this is accomplished by the handle method available to the Kernel class. Once the handle method recognizes an HTTP request, it returns an HTTP response. The Kernel class extends the Illuminate\Foundation\HTTP\Kernel class, which contains a bootstrap method. This method checks if our application has been bootstrapped (the process of starting or resetting ) already. If not, the bootstrap method runs on a bunch of classes to bootstrap the application. It loads the configuration file, also loads the environment variables, handles exceptions, registers facades and also registers the application’s service providers. The application’s default service providers are stored in the app/Providers directory.
The Kernel also defines a list of HTTP middleware that requests needs to pass through before being handled by your application. So what middleware does? It merely filters your requests that are trying to enter your application. For instance, you don’t want to give the user access to some page without being logged in. You can handle that by using middleware in your route files.
One of the most important Kernels bootstrapping actions is loading the service providers for your application. 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.
Service providers are responsible for bootstrapping all of the framework’s various parts, such as the database, queue, validation, and routing components. Since it configures all the features offered by Laravel, it’s the essential component of your Laravel application.
Once the service providers bootstrap the application, the request is now passed to the router for dispatching. The router will dispatch the request to a route file or to a controller to handle the further process. For instance, getting data from database and showing them on a page or storing data to the database.
Now let me give you a step by step request life cycle that will help you visualize the steps mentioned above.
Once the user hit this URL, the browser will send the page request over the Internet to the web server (Apache or Nginx).
The web server will receive the request and analyze the request information. In the web server’s configuration file, the site’s root path is specified. Based on that, the web server will look for index.php file in that root directory, since URL doesn’t contain any subdirectory or any other routes.
Web server will direct the request to public/index.php file of laravel application.
In this step, PHP Interpreter is executing the code contained in the index.php file from the request. During this step, auto loader files which are generated by composer will be loaded.
Then it will create the laravel instance and bootstrapping the components of laravel.
The kernel will receive the request, load the service providers and will be redirected to router.
Router will render the view file content and return back to web server.
Web server receives the output from PHP and sends it back over the Internet to a user’s web browser.
The user’s web browser receives the response from the server, and renders the web page on user’s computer.
I hope this helps you understand the request life cycle. You can also learn more about it from the Laravel documentation.