The goal of this document is to give you a good, high-level overview of how the Laravel framework works. There are nice official articles at their site but my purpose of this article is for the beginners who are joining to Laravel by taking it as their first project and before that they have the curiosity and want to know the internal architecture of Laravel framework.
The Laravel team said in their documents, "By getting to know the overall framework better, everything feels less magical and you will be more confident building your applications" - I am also agree and you will really feel easy all of the terms when you go deep to deeper and explore every features of the Laravel's internal architecture.
Like all of the PHP based project the entry point for all requests to a Laravel application is the index.php which resides inside the public folder. Unlike all of the PHP based project the Laravel team didn't want to place the index.php file in the root folder in where your first time Laravel installation. Rather they have decided to place it inside the public folder where all other user's assets like js, css, favicon etc also co-exists. In this case your project url should be like this, http://your-project-path/public/ . But you are free to move the index.php file to the root folder where you installed Laravel project. So, if you don't want this type of url for your project : http://your-project-path/public/ , Just cut and paste the index.php file and .htaccess file from public folder to root folder and make changes the paths to '/vendor/autoload.php' and '/bootstrap/app.php' in the index.php file. In the .htaccess file change the line to RewriteRule ^ index.php [L]. That's it and you are ready for this url http://your-project-path/ (It seems you are using Xampp or Wamp or like these types application for Apache). For the rest of this article we assume your index.php and .htaccess files has been set in your Laravel project root folder.
All requests are directed to this file (index.php) by your web server (Apache / Nginx) configuration. The index.php file doesn't contain much code. Rather, it is a starting point for loading the rest of the framework. That means when any request come through index.php, Laravel loads all the necessary classes, sub-classes, dependency classes, service classes and all other framework necessary files to give us smooth environment for running our application perfectly. Below are the details what Laravel initially prepare for us before we running our application smoothly.
(A) COMPOSER GENERATED AUTO LOADER DEFINITION
The index.php file loads the Composer generated autoloader definition. In simplicity we can say, Composer generated autoloader files where all related namespaces, classes, functions are defined. Let's look how this happen. If you already installed Laravel go to your installed folder and open the index.php file. There will be a line at 24 : require __DIR__.'/vendor/autoload.php';
The Laravel team said in their documents, "By getting to know the overall framework better, everything feels less magical and you will be more confident building your applications" - I am also agree and you will really feel easy all of the terms when you go deep to deeper and explore every features of the Laravel's internal architecture.
Like all of the PHP based project the entry point for all requests to a Laravel application is the index.php which resides inside the public folder. Unlike all of the PHP based project the Laravel team didn't want to place the index.php file in the root folder in where your first time Laravel installation. Rather they have decided to place it inside the public folder where all other user's assets like js, css, favicon etc also co-exists. In this case your project url should be like this, http://your-project-path/public/ . But you are free to move the index.php file to the root folder where you installed Laravel project. So, if you don't want this type of url for your project : http://your-project-path/public/ , Just cut and paste the index.php file and .htaccess file from public folder to root folder and make changes the paths to '/vendor/autoload.php' and '/bootstrap/app.php' in the index.php file. In the .htaccess file change the line to RewriteRule ^ index.php [L]. That's it and you are ready for this url http://your-project-path/ (It seems you are using Xampp or Wamp or like these types application for Apache). For the rest of this article we assume your index.php and .htaccess files has been set in your Laravel project root folder.
All requests are directed to this file (index.php) by your web server (Apache / Nginx) configuration. The index.php file doesn't contain much code. Rather, it is a starting point for loading the rest of the framework. That means when any request come through index.php, Laravel loads all the necessary classes, sub-classes, dependency classes, service classes and all other framework necessary files to give us smooth environment for running our application perfectly. Below are the details what Laravel initially prepare for us before we running our application smoothly.
(A) COMPOSER GENERATED AUTO LOADER DEFINITION
The index.php file loads the Composer generated autoloader definition. In simplicity we can say, Composer generated autoloader files where all related namespaces, classes, functions are defined. Let's look how this happen. If you already installed Laravel go to your installed folder and open the index.php file. There will be a line at 24 : require __DIR__.'/vendor/autoload.php';
Let's try to understand the process in step by step:
1) All laravel request comes through index.php file
2) In index.php file at line 24 : require __DIR__.'/vendor/autoload.php'; loads the autoload.php file from vendor folder. The vendor directory contains all of the Laravel's Composer dependencies. That means through this starting point of autoload.php file all necessary composer related files are set under vendor folder where the various classes, arrays of file paths are defined that composer can inject them as a dependency to other classes in the application systems files when necessary.
3) In autoload.php file at line 5: require_once __DIR__ . '/composer/autoload_real.php'; includes the autoload_real.php file
4) In autoload_real.php file there is a class generated by composer which main task actually initiate the composer generated autoloader definition by calling static method getLoader() and return the loader in the autoload.php file. The naming convention of this class is something like this:
class ComposerAutoloaderInit3b6cd1a35cc7af982b673ccf692a6425
{
...
public static function getLoader()
{
}
---
}
Inside this class a static method getLoader() defined. Inside this method others composer autoloader files are included and registered like: autoload_static.php, autoload_namespaces.php, autoload_psr4.php, autoload_classmap.php etc.
5) After setting all the necessary dependencies return the $loader to the autoload.php file by calling getLoader() static method in the autoload.php file like the following:
return ComposerAutoloaderInit3b6cd1a35cc7af982b673ccf692a6425::getLoader();
(B) RETRIEVES AN INSTANCE OF THE LARAVEL APPLICATION
After completing loading the Composer generated autoloader definition that composer will use for dependency injection through out the application whenever necessary the index.php file next task is to retrieve an instance of the Laravel application from bootstrap/app.php script. The first action taken by Laravel itself is to create an instance of the application.
Basically in this stage Laravel create a new Laravel application instance which serves as the "glue" for all the components of Laravel, and is the IoC container for the system binding all of the various parts. You see? by creating this instance Laravel actually helping us giving the binding facilities of important interfaces so that we can be able to resolve them when needed. In the Laravel system when we want to go develop application we may need many custom classes and we may need to use these classes as a dependencies to other classes. Laravel has awesome features "Service Container" and "Service Provider" by which we can resolve these dependency classes into another classes using constructor or any setter method. Laravel also defined by default varities "Service Container" registered into the "Service Provider" that we will be able to resolve them when needed Let's understand the things clearly what is going on here.
1) From bootstrap/app.php at line no 14: new Laravel application object created with the initialization of the Illuminate\Foundation\Application class.
$app = new Illuminate\Foundation\Application(
realpath(__DIR__.'/../')
);
2) The $app instance binding important interfaces into the container by registering them through service provider and also $app instance binding important kernel classes and exceptional handler class that we can be able to resolve them when needed. The kernels serve the incoming requests to this application from both the web and CLI. That means when any request comes from either browser or CLI to Laravel system followings are the classes automatically resolved that the new Laravel instance $app has bind for us and no need to initialize them manually. $app instance bind a class or interface using singleton method means the singleton method binds a class or interface into the container that should only be resolved one time. Once a singleton binding is resolved, the same object instance will be returned on subsequent calls into the container. Following are kernel container binding by new Laravel instance $app in the bootstrap/app.php file. These kernel classes interface implementation actually later will help us to redirect our request to router or controller after checking the application components, services and other things.
HTTP Kernel Binding :
$app->singleton(
Illuminate\Contracts\Http\Kernel::class,
App\Http\Kernel::class
);
Console Kernel Binding :
$app->singleton(
Illuminate\Contracts\Console\Kernel::class,
App\Console\Kernel::class
);
Exception Handler Binding :
$app->singleton(
Illuminate\Contracts\Debug\ExceptionHandler::class,
App\Exceptions\Handler::class
);
3) After creating new Laravel application instance and binding important interfaces the /bootstrap/app.php return the application to the index.php file
$app = require_once __DIR__.'/bootstrap/app.php';
BEFORE GOING FURTHER TO LEARN LARAVEL WORK FLOW LET’S REPEAT WHAT WE HAVE ALREADY GRABBED FROM THE ABOVE
1) The entry point for all requests to a Laravel application is the index.php file
2) The index.php file loads the Composer generated autoloader definition using
require __DIR__.'/vendor/autoload.php';
3) autoload.php file loads the actual ComposerAutoloaderInit class in which a static method getLoader() defined
4) getLoader() method return the necessary data by loading other important composer auto loading files and autoload.php receive this static method information and return it. In this phase all necessary auto loader registered to the system
5) Next, bootstraping the framework by creating a new Laravel application instance using
$app = require_once __DIR__.'/bootstrap/app.php';
6) The new Laravel application instance created by instantiating the following:
$app = new Illuminate\Foundation\Application(
realpath(__DIR__.'/../')
);
7) bootstrap/app.php also bind some important interfaces using Laravel "Service Container" and "Service Provider" mechanism. These kernels serve the incoming requests to this application from both the web and CLI.
$app->singleton(
Illuminate\Contracts\Http\Kernel::class,
App\Http\Kernel::class
);
$app->singleton(
Illuminate\Contracts\Console\Kernel::class,
App\Console\Kernel::class
);
$app->singleton(
Illuminate\Contracts\Debug\ExceptionHandler::class,
App\Exceptions\Handler::class
);
8) Next, return the $app to the index.php file
1) All laravel request comes through index.php file
2) In index.php file at line 24 : require __DIR__.'/vendor/autoload.php'; loads the autoload.php file from vendor folder. The vendor directory contains all of the Laravel's Composer dependencies. That means through this starting point of autoload.php file all necessary composer related files are set under vendor folder where the various classes, arrays of file paths are defined that composer can inject them as a dependency to other classes in the application systems files when necessary.
3) In autoload.php file at line 5: require_once __DIR__ . '/composer/autoload_real.php'; includes the autoload_real.php file
4) In autoload_real.php file there is a class generated by composer which main task actually initiate the composer generated autoloader definition by calling static method getLoader() and return the loader in the autoload.php file. The naming convention of this class is something like this:
class ComposerAutoloaderInit3b6cd1a35cc7af982b673ccf692a6425
{
...
public static function getLoader()
{
}
---
}
Inside this class a static method getLoader() defined. Inside this method others composer autoloader files are included and registered like: autoload_static.php, autoload_namespaces.php, autoload_psr4.php, autoload_classmap.php etc.
5) After setting all the necessary dependencies return the $loader to the autoload.php file by calling getLoader() static method in the autoload.php file like the following:
return ComposerAutoloaderInit3b6cd1a35cc7af982b673ccf692a6425::getLoader();
(B) RETRIEVES AN INSTANCE OF THE LARAVEL APPLICATION
After completing loading the Composer generated autoloader definition that composer will use for dependency injection through out the application whenever necessary the index.php file next task is to retrieve an instance of the Laravel application from bootstrap/app.php script. The first action taken by Laravel itself is to create an instance of the application.
Basically in this stage Laravel create a new Laravel application instance which serves as the "glue" for all the components of Laravel, and is the IoC container for the system binding all of the various parts. You see? by creating this instance Laravel actually helping us giving the binding facilities of important interfaces so that we can be able to resolve them when needed. In the Laravel system when we want to go develop application we may need many custom classes and we may need to use these classes as a dependencies to other classes. Laravel has awesome features "Service Container" and "Service Provider" by which we can resolve these dependency classes into another classes using constructor or any setter method. Laravel also defined by default varities "Service Container" registered into the "Service Provider" that we will be able to resolve them when needed Let's understand the things clearly what is going on here.
1) From bootstrap/app.php at line no 14: new Laravel application object created with the initialization of the Illuminate\Foundation\Application class.
$app = new Illuminate\Foundation\Application(
realpath(__DIR__.'/../')
);
2) The $app instance binding important interfaces into the container by registering them through service provider and also $app instance binding important kernel classes and exceptional handler class that we can be able to resolve them when needed. The kernels serve the incoming requests to this application from both the web and CLI. That means when any request comes from either browser or CLI to Laravel system followings are the classes automatically resolved that the new Laravel instance $app has bind for us and no need to initialize them manually. $app instance bind a class or interface using singleton method means the singleton method binds a class or interface into the container that should only be resolved one time. Once a singleton binding is resolved, the same object instance will be returned on subsequent calls into the container. Following are kernel container binding by new Laravel instance $app in the bootstrap/app.php file. These kernel classes interface implementation actually later will help us to redirect our request to router or controller after checking the application components, services and other things.
HTTP Kernel Binding :
$app->singleton(
Illuminate\Contracts\Http\Kernel::class,
App\Http\Kernel::class
);
Console Kernel Binding :
$app->singleton(
Illuminate\Contracts\Console\Kernel::class,
App\Console\Kernel::class
);
Exception Handler Binding :
$app->singleton(
Illuminate\Contracts\Debug\ExceptionHandler::class,
App\Exceptions\Handler::class
);
3) After creating new Laravel application instance and binding important interfaces the /bootstrap/app.php return the application to the index.php file
$app = require_once __DIR__.'/bootstrap/app.php';
BEFORE GOING FURTHER TO LEARN LARAVEL WORK FLOW LET’S REPEAT WHAT WE HAVE ALREADY GRABBED FROM THE ABOVE
1) The entry point for all requests to a Laravel application is the index.php file
2) The index.php file loads the Composer generated autoloader definition using
require __DIR__.'/vendor/autoload.php';
3) autoload.php file loads the actual ComposerAutoloaderInit class in which a static method getLoader() defined
4) getLoader() method return the necessary data by loading other important composer auto loading files and autoload.php receive this static method information and return it. In this phase all necessary auto loader registered to the system
5) Next, bootstraping the framework by creating a new Laravel application instance using
$app = require_once __DIR__.'/bootstrap/app.php';
6) The new Laravel application instance created by instantiating the following:
$app = new Illuminate\Foundation\Application(
realpath(__DIR__.'/../')
);
7) bootstrap/app.php also bind some important interfaces using Laravel "Service Container" and "Service Provider" mechanism. These kernels serve the incoming requests to this application from both the web and CLI.
$app->singleton(
Illuminate\Contracts\Http\Kernel::class,
App\Http\Kernel::class
);
$app->singleton(
Illuminate\Contracts\Console\Kernel::class,
App\Console\Kernel::class
);
$app->singleton(
Illuminate\Contracts\Debug\ExceptionHandler::class,
App\Exceptions\Handler::class
);
8) Next, return the $app to the index.php file
(C) RUN THE APPLICATION
Once we have the application instance $app, we can handle the incoming request through the kernel. 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.
Though these two kernels (HTTP and Console) are binding using service provider and service container, which one request is entering the application depends on it one will be automatically resolved.
The HTTP kernel extends the Illuminate\Foundation\Http\Kernel class, which defines an array of bootstrappers that will be run before the request is executed. These bootstrappers configure error handling, configure logging, detect the application environment, and perform other tasks that need to be done before the request is actually handled.
The HTTP kernel also defines a list of HTTP middleware that all requests must pass through before being handled by the application. These middleware handle reading and writing the HTTP session, determining if the application is in maintenance mode, verifying the CSRF token, and more.
Look at the code below in the index.php file
$kernel = $app->make(Illuminate\Contracts\Http\Kernel::class);
$response = $kernel->handle(
$request = Illuminate\Http\Request::capture()
);
$response->send();
$kernel->terminate($request, $response);
The method signature for the HTTP kernel's handle method is quite simple: receive a Request and return a Response. Think of the Kernel as being a big black box that represents your entire application. Feed it HTTP requests and it will return HTTP responses.
(D) DISPATCH REQUEST
Once the application has been bootstrapped and all service providers have been registered, the Request will be handed off to the router for dispatching. The router will dispatch the request to a route or controller, as well as run any route specific middleware for filtering the request.
Hope this article will help you if you want to understand the Laravel architectural concepts. For more details I will suggest you to read the official site of Laravel though there are many many things in their good documentation. Thanks.
Once we have the application instance $app, we can handle the incoming request through the kernel. 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.
Though these two kernels (HTTP and Console) are binding using service provider and service container, which one request is entering the application depends on it one will be automatically resolved.
The HTTP kernel extends the Illuminate\Foundation\Http\Kernel class, which defines an array of bootstrappers that will be run before the request is executed. These bootstrappers configure error handling, configure logging, detect the application environment, and perform other tasks that need to be done before the request is actually handled.
The HTTP kernel also defines a list of HTTP middleware that all requests must pass through before being handled by the application. These middleware handle reading and writing the HTTP session, determining if the application is in maintenance mode, verifying the CSRF token, and more.
Look at the code below in the index.php file
$kernel = $app->make(Illuminate\Contracts\Http\Kernel::class);
$response = $kernel->handle(
$request = Illuminate\Http\Request::capture()
);
$response->send();
$kernel->terminate($request, $response);
The method signature for the HTTP kernel's handle method is quite simple: receive a Request and return a Response. Think of the Kernel as being a big black box that represents your entire application. Feed it HTTP requests and it will return HTTP responses.
(D) DISPATCH REQUEST
Once the application has been bootstrapped and all service providers have been registered, the Request will be handed off to the router for dispatching. The router will dispatch the request to a route or controller, as well as run any route specific middleware for filtering the request.
Hope this article will help you if you want to understand the Laravel architectural concepts. For more details I will suggest you to read the official site of Laravel though there are many many things in their good documentation. Thanks.