Laravel is a powerful framework, and the Laravel request lifecycle is the best place to start when you want to learn more about the framework itself. An in-depth examination of the request lifecycle will aid our understanding of the Laravel structure.
- All requests into your application are directed from the user to Webserver through the
public/index.php
script. When using Apache, the.htaccess
file that ships with Laravel handles the passing of all requests toindex.php
. - Then it retrieves an instance of the Laravel application from
bootstrap/app.php
script. Laravel itself creates an instance of the application, which is the initial/first step. - After that, The incoming request is sent to either the HTTP kernel or the console kernel, depending on the type of request that is entering the application . These two kernels serve as the central location that all requests flow through. HTTP kernel, which is placed in
app/Http/Kernel.php
. It just receives a Request and returns a Response. Bootstrappers are defined by the Kernel class, which configures error handling, configures logging, detects environments and other tasks to be done before the request is handled. HTTP Kernel will define the list of middleware that is passed through before being handled by the application. - The most important concept to grasp when learning about Laravel’s bootstrap process is Service Providers. You can find a list of service providers by opening your
app/config/app.php
configuration file and finding theproviders
array. These providers serve as the primary bootstrapping mechanism for Laravel. But, before we dig into service providers, let's go back toindex.php
. After a request enters yourindex.php
file, thebootstrap/start.php
file will be loaded. This file creates the new LaravelApplication
object, which also serves as an IoC container. - Now request will be dispatched by the Router and it will end up with the views as shown below:
- Router will direct the HTTP Request to a Controller or return a view or responses directly by omitting the controller. These routes will be placed in
app/routes.php.
- Controller
app/controllers/
performs specific actions and sends data to a View. - View
app/views/
formats the data appropriately, providing the HTTP Response.