Just like all PHP web applications, the entry point for every request in a Laravel application is public/index.php. There isn't much content in this file — it simply loads the Laravel framework. What does it do exactly? It loads the class definitions auto-generated by Composer, then retrieves the application instance from bootstrap/app.php, which sets up the base directories and binds the incoming request to the Laravel application instance.
Next, the request is handed off to the Kernel. The one you'll encounter most often when starting out is app/Http/Kernel.php. This file handles six things:
- Load the
.envfile - Load configuration settings
- Set up error handling
- Set up Facades
- Register service providers (i.e., the
registermethod) - Boot service providers (i.e., the
bootmethod)
The thing you need to pay the most attention to here is registering and booting service providers, because Laravel's routing mechanism is implemented through the RouteServiceProvider. Once the route service provider has been registered, the request is handed over to the router for processing.
From here on, it's the part most developers are more familiar with. The router matches the request path against its rules and determines which controller method should handle it. Along the way, the request is guided through all the middleware defined on the route — only after passing through every middleware layer does the request finally reach the controller method. Once the controller method finishes processing and returns a response, that response must also pass back through the route's middleware chain. After clearing all middleware, index.php calls the response's send() method, which delivers the response content to the user's web browser.
That's the entire request lifecycle flow. If you don't fully understand it yet, start by focusing on service providers.