Understanding the Life Cycle of a Laravel Request

Hasan Masud
3 min readJan 17, 2023

--

Laravel is a popular web application framework that makes building applications easier and faster. But how exactly does the life cycle of a Laravel request work? Understanding the life cycle of a Laravel request is the key to understanding how it works and how to best utilize its features.

Laravel Request Lifecycle
Image courtesy: bytefum

Application entry point

When a Laravel application is hosted on a web server such as Apache or Nginx, the web server will direct all requests to the public/index.php file. This file acts as the starting point for the application, and all requests to the application are routed through it.

public/index.php performs following tasks:

  • It first loads vendor/autoload.php file (generated by composer.json file)
  • Then it includes bootstrap/app.php file (creates create an instance of the application)
  • The request object is passed to the Kernel class (Http/Kernel.php or Console/Kernel.php depending on the type of request. Web server/Terminal)
  • Hands the request to Kernel class and receives the Response class and terminate the request

Http/Console Kernel

Following this step, the Laravel Kernel determines whether the request is coming from a browser (an HTTP request) or from the command line (a console request).

Depending on the type of request, the Kernel will then direct the request to the appropriate Kernel.

  • For an HTTP request, the Kernel will be the app/Http/Kernel.php file.
  • For a console request, the Kernel will be the app/Console/Kernel.php file.

Http Kernel

Once the appropriate Kernel is identified, the Kernel will act as a “black box” and represent the entire application. It will perform several task, including:

  • Bootstrapping the application
  • Configuring error handling and logging
  • Detecting the environment
  • Registering and bootstrapping service providers
  • Takes the Request object and respond with Response object

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

What Service Provider does in Laravel?

Service providers are responsible for bootstrapping various components of Laravel framework, such as:

  • Database connection
  • Queues
  • Validations
  • Routing components etc.

Additionally, service providers allow you to load your own migrations, models, controllers, routes, and configurations.

The register() method of the service provider is called before the boot() method. This is because it allows developers to register any bindings they need before the boot method is executed. It is important to note that the order of execution is register first and then boot.

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

Router

During bootstrapping process it boots RouteServiceProvider which performs following tasks:

  • 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
  • The controller, located at app/controllers/ perform following tasks:
  • Route validation
  • Form validation
  • Run any business logic or communicating with models
  • It takes some params and pass down to view
  • and view is rendered and creates Response object
  • Response object is then delivered back to Kernel

Finishing Up

Finally, the Response object is sent back through the middlewares, giving the application a chance to modify the response. The public/index.php file then receives the response and calls the send() method on the Response object, which sends the response content to the client’s web browser. This completes the life cycle of the request and response in Laravel.

The life cycle of a Laravel request involves a series of steps, each of which is essential for the proper functioning of the application. Starting with the public/index.php entry point, the process progresses through routing, controllers, models, views, and ultimately ends with the Response object. Understanding the intricacies of each step helps developers create more efficient and powerful applications.

--

--

Hasan Masud
Hasan Masud

Written by Hasan Masud

Full-Stack Developer w/ expertise in PHP, MySQL, & JavaScript. Learning Python & Django for process automation. Experienced across all layers of SDLC.

No responses yet