Skip to main content

Understanding the Application Lifecycle

3/28
Chapter 1 Getting Started with Laravel 12

Understanding the Application Lifecycle

18 min read Lesson 3 / 28

How a Laravel Request Works

Understanding the request lifecycle helps you debug issues and architect better applications. Every HTTP request follows this path through Laravel.

The Request Journey

  1. Entry Pointpublic/index.php receives every request
  2. Bootstrap — Laravel loads the service container, registers providers, and boots the application
  3. HTTP Kernel — The request passes through global middleware (CORS, maintenance mode, cookie encryption)
  4. Router — Matches the URL to a route definition
  5. Route Middleware — Applies route-specific middleware (auth, throttle)
  6. Controller — Your code handles the request
  7. Response — Returns HTML, JSON, or a redirect

Service Container

The service container is Laravel's powerful dependency injection system:

// Laravel automatically resolves dependencies
class PostController extends Controller
{
    // PostService is automatically injected
    public function __construct(
        private PostService $postService
    ) {}

    public function index(): View
    {
        $posts = $this->postService->getPublished();
        return view('posts.index', compact('posts'));
    }
}

Service Providers

Service providers bootstrap application services. They are registered in bootstrap/providers.php:

// app/Providers/AppServiceProvider.php
class AppServiceProvider extends ServiceProvider
{
    public function register(): void
    {
        // Bind interfaces to implementations
        $this->app->bind(
            PaymentGateway::class,
            StripePaymentGateway::class
        );
    }

    public function boot(): void
    {
        // Run after all providers are registered
        Model::preventLazyLoading(! app()->isProduction());
    }
}

Configuration Caching

In production, cache your configuration for performance:

php artisan config:cache   # Cache config files
php artisan route:cache    # Cache route definitions
php artisan view:cache     # Pre-compile Blade templates
php artisan optimize       # All of the above

Understanding this lifecycle means you know exactly where to add custom logic — middleware for request filtering, service providers for bindings, and controllers for business logic.