Understanding how Laravel works
Using Laravel it would be good to understand what is going on inside the framework. Let’s follow typical request step by step to look closer how Laravel works.
Any request is pointed to front controller index.php file located in public directory. There are 6 steps to fulfield coming request by preparing and sending the response.
Step 1: Autoloading
First, autoload file created by the composer is loaded. Nothing Laravel – magical till this point.
Step 2: App instance
$app = require_once __DIR__.'/../bootstrap/app.php';
Next lights are turned on – bootstrap/app.php file is loaded. The aim of that file is to return fresh Laravel Application instance. When new app instance is created inside Application class main Container bindings are registered as well as Event, Log and Routing Service Providers and core aliases.
After that 3 important and early needed classes are registered in Container as singletons: Http Kernel, Console Kernel and Exception Handler.
Finally, app instance is returned to index.php. By the way – bootstrap/app.php file can always be called to create another fresh app instance. For example, tests/CreatesApplication.php file does that for every test.
Step 3: Kernel instance
$kernel = $app->make(Illuminate\Contracts\Http\Kernel::class);
Http Kernel is already registered so an app can create Kernel instance when a related contract is passed to make() method. A Kernel is main class responsible for running Laravel magic. There is very similar approach when Laravel is fired from the console – artisan file is run and instead of Http Kernel instance of Console Kernel is created.
Step 4: Response instance
$response = $kernel->handle( $request = Illuminate\Http\Request::capture() );
A lot is going on in that step. First, the request is created from capture() method. It uses Symfony classes to build request object. Then handle() method of Kernel object is called – this is core method of the framework. Inside Kernel:
- Arrays of middleware are attached to the router.
- The request is bind to Container.
- The framework is bootstrapped! Before that moment Laravel cannot do much. To bootstrap framework means that env variables are loaded, then the configuration is loaded, the Exception Handler is bind to Container, Facades are registered and finally Servis Providers are loaded what adds features to the framework.
- Global middleware are run.
- The router is dispatched with the request and return the response.
Step 5: Sending response
Symfony comes to play again to help Laravel send a response to the browser.
Step 6: Clean up
If there is any route middleware or any request middleware (defined in Http Kernel file) with terminate() method (like Session middleware for example), that method will be invoked.
Laravel request lifecycle is less difficult that we could expect. The framework offers a lot of features and that sounds complicated but at the end of the day, this is just PHP code, well designed, easy to read and understand. Just like hole request lifecycle.