What is Bootstrapping in Laravel and how does it work?
3 Answers
I will try my best to simplify Laravel's Bootstrapping process. Some terms to understand before we focus on bootstrapping process:
Service Container - holds different components which can be used in your application
Facades - Laravel has lots of features and helpers. For example helpers regarding URL, Response, Route, Session, Request etc. These are loaded into service container. Facsdes makes it possible to access these features without going through service container, and can be accessed directly.
Middleware - is a code executed on each request. For example, setting up the session. cookie encryption etc. Middleware is required by each request.
Service Providers - are classes required by your application. These are not loaded on each request, but only when the service they provide are actually needed.
Kernel - is responsible for loading all the middleware, service providers
When a web page is requested, the request first goes to index.php. This file does very important things. First it auto-loads all the classes used in your application.
After that it sets up the laravel application i.e. laravel framework. One of the first thing laravel framework does is - it creates the service container. At this point the service container is empty.
After that framework creates kernal. Kernal loads all the middleware required by the application. One of the most important task of the kernal, there after, is to load all the service providers i.e. to load all the components into the service container.
The list of all the service providers loaded are in the config/app.php file i.e. providers array. This array defines which components are loaded into your service container.
These components can be accessed via facades. The list of all facades are also present in the config/app.php as aliases array. These facades are simply short-cuts to all the components loaded into the service container. This finishes the bootstrap process.
After all this, request is then handed over to the router i.e. routes/web.php file.
Bootstrapping is the process Laravel takes to combine the necessary bits in the framework together to be able to process and handle the functionality thrown at the system.
Inside the bootstrap folder you will find a few files:autoload.php - loads and includes composer so any packages are loaded.services.php - stores the providers in the application to load the system quicker.app.php - loads and initialises the app.
As above, all of this is easily discoverable online.
Before a request can be handled, certain dependencies or parts of the Laravel application have to be setup. These parts allow for the request to be processed as intended by the application, allowing as well for a response to generated appropriately. Bootstrapping is the process of setting all these things up.
In a bit technical terms as per service providers documentation, "service providers" is the name that is used to refer to the different classes (chunked up pieces of code with a pre-defined outcome) that will be used to register and boot the required functionality for the Laravel application to work properly.