Skip to content
Tutorials Center
My learning

Topics

Lesson 4 of 67 ยท Getting Started

The Request Lifecycle

Follow a request through Laravel 13: index.php, bootstrap, providers, middleware, router and response, with a runnable mini front controller.

When someone opens a page of your Laravel site, a lot happens in a few milliseconds: the web server starts PHP, Laravel boots, your request passes through a chain of middleware, the router finds your code, your code returns something, and the response travels back through the same chain. You do not need to memorize the internals, but knowing the order explains many things that otherwise feel magical: why middleware runs before your controller, why config() works everywhere, and why a slow boot() method slows down every page.

This lesson describes the lifecycle in Laravel 13, then builds a miniature version in plain PHP so you can watch the order of events for yourself. The Laravel parts are read-only; the stand-in is not real Laravel, but it follows the same sequence.

The big picture

sequenceDiagram
    participant B as Browser
    participant W as Web server
    participant I as public/index.php
    participant A as Application
    participant M as Middleware
    participant R as Router
    participant C as Controller
    B->>W: GET /posts/7
    W->>I: run PHP
    I->>A: bootstrap and create Request
    A->>M: handle(request)
    M->>R: dispatch to a route
    R->>C: call action
    C-->>R: return value
    R-->>M: Response
    M-->>A: Response
    A-->>B: send headers and body
    A->>A: terminate (after the response)

One request in, one response out, with middleware wrapped around the router.

Step 1: public/index.php

The web server is configured to send every request that is not a real file to public/index.php. This is the front controller. In a new project, it is only a few lines (read-only):

PHP
<?php

use Illuminate\Http\Request;

define('LARAVEL_START', microtime(true));

// Determine if the application is in maintenance mode...
if (file_exists($maintenance = __DIR__.'/../storage/framework/maintenance.php')) {
    require $maintenance;
}

// Register the Composer autoloader...
require __DIR__.'/../vendor/autoload.php';

// Bootstrap Laravel and handle the request...
(require_once __DIR__.'/../bootstrap/app.php')
    ->handleRequest(Request::capture());

It does four things: checks for maintenance mode, loads Composer's autoloader, loads bootstrap/app.php (which returns the application, see The Project Structure), and hands it a Request object built from PHP's superglobals.

Step 2: bootstrapping

The application object is also the service container, the box that builds and stores services (The Service Container and Dependency Injection). Before handling the request, Laravel runs a series of bootstrappers: it loads the .env file, loads configuration from config/, sets up error handling, registers facades and then loads the service providers.

Providers work in two passes. First every provider's register() method runs, which only binds things into the container. Then every provider's boot() method runs, when all services exist and can use each other. The list of providers lives in bootstrap/providers.php; the framework's own providers are included automatically. Details are in Service Providers, Facades and Contracts.

Step 3: middleware

Next the request goes through middleware, small classes that wrap the rest of the application. Typical jobs are starting the session, encrypting cookies, checking the CSRF token, and checking that the user is logged in. Each middleware can:

  • stop the request and return a response early (for example a redirect to the login page), or
  • pass the request on, then change the response on the way back.

Global middleware runs for every request, and group or route middleware runs only where you attach it. See Middleware for the details. The idea is an onion: the request goes in through the layers, and the response comes out through them in reverse order.

Step 4: the router

At the centre of the onion, the router matches the URL and HTTP method against the routes you defined in routes/web.php. It extracts parameters (the 7 in /posts/7), resolves model bindings, and calls the matched closure or controller method. If nothing matches, Laravel throws a not-found exception, which becomes a 404 page.

Controller dependencies are provided by the container: if a method asks for a Request or a service by its type, Laravel builds it. Routing Basics and Controllers go deeper.

Step 5: the response

Whatever your route returns is turned into a response object. A string becomes an HTML response, an array or model becomes JSON, and a view becomes rendered HTML. The response then travels back out through the middleware, and Laravel sends the status line, headers and body to the browser.

Step 6: terminate

After the response is sent, Laravel runs terminable middleware and similar clean-up code. Work done here does not delay the user. Queued jobs and the scheduler exist for heavier work, covered in Queues and Jobs.

Try it: a mini front controller

This stand-in has the same parts: a Request, a list of middleware around a router, a Response, and a terminate step. It logs each stage so you can see the order. Real Laravel is far more capable, but if you can read this you can read the real flow.

PHP A mini front controller

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.

The log shows the onion: before runs first, then the router, then after. Now see what happens when no route matches, and when a middleware stops the request early:

PHP Not found and early exit

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.

The guard never calls $next for /admin, so the router never runs. Real Laravel middleware works exactly this way: returning a response without calling $next($request) ends the request there. The PHP middleware pipeline lesson builds the same idea step by step.

Why this matters in practice

  • Slow pages: expensive work in a provider's boot() runs on every request. Keep it light.
  • config() and env(): configuration is loaded in the bootstrap step, so it is available everywhere afterwards. See Configuration and Environments.
  • Debugging: if code does not run, ask which stage it belongs to. A redirect to login came from middleware, a 404 from the router, a 500 from your own code or an exception.
  • Cached bootstraps: commands such as php artisan config:cache and route:cache speed up steps 2 and 4. They are covered in Performance and Optimization.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which file receives every web request in a Laravel application?

    public/index.php is the front controller. The web server sends all requests to it, and it loads and runs the application.
  2. 2. What is the difference between a provider's register() and boot() methods?

    Registration happens first for every provider, so by the time boot() runs, every service is available.
  3. 3. A middleware returns a response without calling $next($request). What happens?

    Not calling the next layer ends the journey inward. The response travels back out through the middleware that already ran.
  4. 4. When does terminable middleware run?

    Terminating work runs after the response has been sent, so it does not delay the user.

Summary

  • Every request enters through public/index.php, which loads bootstrap/app.php and hands a Request to the application.
  • Bootstrapping loads the environment, configuration and service providers (register first, then boot).
  • Middleware wraps the router like an onion and can stop a request early.
  • The router matches the URL, resolves parameters and bindings, and calls your closure or controller.
  • Return values become responses, which flow back out through the middleware; terminable work runs afterwards.

Next: Configuration and Environments