Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
Request–Response Flow and Project Folders  ›  Lesson

The Request–Response Flow

Request–Response Flow 12 minEvery request enters through public/index.php, the app is built from bootstrap/app.php, passes through middleware to the router and a controller, and the response travels back out through the same middleware.

The Request–Response Flow

A member opens http://lendly.test/books, and a list of books appears. Between those two moments, Laravel runs the request through a fixed series of stops. Once you know the order, every file you meet later has a place: "this runs before routing", "this runs inside the controller".

The rule

One door in, middleware around, router in the middle, same way out.

Follow the request:

  1. public/index.php: the web server sends every request here. It is the only PHP file the outside world can reach.
  2. bootstrap/app.php: index.php loads Composer's autoloader, then asks this file to build the application. This is where routing files, middleware and exception handling are configured.
  3. Boot: Laravel loads configuration, sets up error handling and runs the service providers, which register the app's pieces.
  4. Global middleware: layers that every request passes through, such as trimming whitespace from input or showing a "back soon" page while the site is in maintenance mode.
  5. Router: matches the method and path against routes/web.php, runs that route's own middleware, such as sessions and CSRF protection, then calls the closure or controller.
  6. Response: whatever the controller returns, whether a view, JSON or a redirect, is turned into a response object.
  7. Back out: the response passes back through the same middleware in reverse, then index.php sends it to the browser.

In Laravel 11 and 12, bootstrap/app.php is short and readable:

return Application::configure(basePath: dirname(__DIR__))
    ->withRouting(
        web: __DIR__.'/../routes/web.php',
        commands: __DIR__.'/../routes/console.php',
        health: '/up',
    )
    ->withMiddleware(function (Middleware $middleware) {
        //
    })
    ->withExceptions(function (Exceptions $exceptions) {
        //
    })->create();

The shortcut

Picture an onion. The controller is the centre, and middleware layers wrap it. A request cuts inward through every layer, and the response comes back out through the same layers. When something happens "to every request", look at the layers. When it happens "on one page", look at the centre.

GET /books, stop by stopStep 1 of 5
  1. The door

    public/index.php receives the request.

The trap is thinking each route file is its own entry point. There is only one door, public/index.php, and routes are just a lookup table the router checks.

Drills

Answer fast — trust the rule.

quick drill

Which file receives every web request first?

quick drill

Where do you configure middleware and routing files in Laravel 11 or 12?

quick drill

Which comes first for a request to /books?

quick drill

What happens to the response after the controller returns it?

Remember

  • One door in, middleware around, router in the middle, same way out.
  • Picture the onion: "every request" means middleware, "one page" means controller.
  • Trap: route files are a lookup table, not entry points.

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainTrace one request

A member opens http://lendly.test/books. Trace the request through Laravel from the moment it arrives until HTML is sent back, naming each stop in order.