Laravel Request Response Life Cycle: Laravel Architectural Concepts

Mohasin Hossain
3 min readFeb 6, 2022

--

Request life cycle of Laravel

You can see the request life cycle in the diagrammatical view above. Let’s explain.

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). The index.php file is a starting point for loading the rest of the framework.

It auto loads the auto loader files via bootstrap/autoload.phpwhich is generated by composer.

Then it retrieves an instance of the Laravel application frombootstrap/app.php file. Laravel itself creates an instance of the application.

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 into 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 routes/

Controller app/controllers/ performs specific actions and sends data to a View.

View resource/views/ formats the data appropriately, providing the HTTP Response.

After sending response kernel terminate the request. And that’s all about request life cycle of Laravel.

Service Container

It is basically the feature that does the dependency resolution / dependency injections. It contains all the bindings that we want to inject as dependency.

Service Provider

Basically a module registry system. Let’s say we want to organize our dependency bindings, middlewares, event listeners etc, then we create a service provider and register related things in it. So even though the Service Container is responsible for injecting dependencies, we actually bind the dependencies in Service Provider.

Facade

Some build in static classes that we can use easily to do common tasks such as access session, cache etc.

Please feel free to leave a comment if you have any feedback, questions or want me to write about another PHP/Laravel related topic.

--

--

Mohasin Hossain

Senior Software Engineer | Mentor @ADPList | Backend focused | PHP, JavaScript, Laravel, Vue.js, Nuxt.js, MySQL, TDD, CI/CD, Docker, Linux