Laravel 8 Request/Response LifeCycle

If you are working on laravel project it is very crucial to understand the lifecycle of laravel. If we are clear about how it works and what laravel features are available we can really become a good laravel developer.

In this tutorial I will teach you how laravel works and during each phase of the lifecycle what laravel features we can use and write better code.

Laravel application entry point

When laravel application runs on top of your webserver. Your webserver points to public/index.php file. All laravel requests go to public/index.php file. This file is a starting point of the application.

What does public/index.php do:

  • It first loads vendor/autoload.php -> file generated by your composer.json file
  • then it includes bootstrap/app.php file -> creates  create an instance of the application
  • Than it creates request object calls the appropriate Kernel class in our case Http/Kernel.php
  • Hands the request to Kernel class and recieves the Response class and terminate the request

Laravel Http/Console Kernel

In the next phase, laravel Kernel decide weather request is coming from the browser (i.e. http request) or is it coming from command line (i.e. console request) depending on the request laravel Kernel redirect request to appropriate Kernel:

  • app/Http/Kernel.php if request is coming from web/browser
  • app/Console/Kernel.php if request is coming from terminal or command prompt

For purpose of our tutorial we would talk about Http Request. Let say our request is coming from browser and hitting app/Http/Kernel.php file.

Laravel Http Kernel

Think of the kernel as being a big black box that represents your entire application. It does following tasks:

  • It bootstraps your application and configures error handling, loging, detect env etc...
  • Takes the Request object and respond with Response object

HTTP Kernel will define the list of middleware that are passed through before handled by application. One of the most important task of Kernel bootstrapping process is to register and boot service providers.

According to Laravel doc:

Laravel will iterate through this list of providers and instantiate each of them. After instantiating the providers, the register method will be called on all of the providers. Then, once all of the providers have been registered, the boot method will be called on each provider.

What is Service Provider in Laravel?

Service providers are responsible for bootstrapping various components of Laravel framework for example:

  • Database connection
  • Queues
  • validations
  • routing components etc...

Service provider is the best place to load new features if you want to customize your application to your need. You can create your own laravel package using single service provider file in your package.

Service providers allows you to load your own migrations, models, controllers, routes, configurations etc... Important thing to note here is that register method of the service provider is called first and then boot method.

Reason why register method is called first is because it allows you to register any bindings you need before it runs the boot method.

Next, once all service providers are bootstrapped it will hand over request to router for dispatching.

Laravel Router

During bootstrapping process it boots RouteServiceProvider which is reponsible for:

  • loading route files
  • register all routes
  • it takes the request and goes through all route middlewares
  • after request goes through registered route middlewares it hits the controller
  • controller is reponsible for
    • route validation
    • form validation
    • run any business logic or talk to models
    • it takes some params and pass down to view
    • and view is rendered and creates Response object
    • Response object is then delievered back to Kernel

Finishing Up the Response

Finally, once response is returned back it goes through middlewares, giving application a chance to modify the reponse. Now, public/index.php file receives the response and calls send method on Response object.

The send method sends the response content to the client's web browser and that's how laravel completes the life cycle of the request and response.