Skip to content

Lesson 2 of 9 · 20m

Request Lifecycle, Configuration, and Service Providers

How a request actually travels through Laravel, what providers are for, and why configuration discipline prevents deploy-time surprises.

A senior Laravel developer can narrate the request lifecycle without opening the framework source. Not because trivia is fun, but because lifecycle knowledge explains half the weird production bugs: stale config, missing bindings, middleware in the wrong order, and code that works locally but never on workers.

The path of a web request

public/index.php
  -> bootstrap/app.php
  -> HTTP kernel
  -> global middleware
  -> route middleware
  -> controller/action
  -> response through middleware in reverse

A console command or queued job does not take the HTTP route. It boots the same application container, but no request object exists in the same way and session middleware never runs. Code that secretly assumes request()->user() exists will fail the moment it moves into a job.

Providers are the wiring layer

config/app.php historically listed providers; Laravel 11+ applications usually register application providers in bootstrap/providers.php and customize behavior in AppServiceProvider. Package providers are discovered through Composer metadata. Keep application wiring explicit:

final class AppServiceProvider extends ServiceProvider
{
    public function register(): void
    {
        $this->app->bind(UsageMeter::class, DatabaseUsageMeter::class);
    }

    public function boot(): void
    {
        Model::shouldBeStrict(! app()->isProduction());
    }
}

register() wires. boot() uses. Mixing them causes ordering problems because every provider is registered before any provider is booted.

Configuration belongs in config files

// config/services.php
return [
    'stripe' => [
        'key' => env('STRIPE_KEY'),
        'webhook_tolerance' => env('STRIPE_WEBHOOK_TOLERANCE', 300),
    ],
];

env() outside config files breaks once you run php artisan config:cache, because the cached config is frozen and .env is no longer read on every request. In production you should cache configuration as part of deployment:

php artisan config:cache
php artisan route:cache
php artisan view:cache

Middleware order is behavior

Authentication cannot run before the session starts. CSRF validation cannot inspect input that middleware has already mutated. If a custom middleware needs the authenticated user, place it after Laravel's session and authentication middleware. Do not fix ordering bugs by stuffing logic into service providers.

Field notes

  • Never deploy a config change without clearing and rebuilding cached config; stale config is indistinguishable from a broken deploy.
  • Routes with closures cannot be route-cached. Use invokable controllers instead.
  • Deferred providers are useful for packages, but application services are usually cheap enough to keep explicit and simple.
  • If behavior differs between web and queue contexts, inspect the lifecycle first before blaming the queue.

Was this lesson helpful?

Sign in to rate