SoFunction
Updated on 2025-04-04

Analysis of the principle of entry file in Laravel framework source code analysis

This article describes the entry file principle of source code analysis of Laravel framework. Share it for your reference, as follows:

Preface

The way to improve your abilities is not to use more tools, but to decipher the tools you use. Today we will start with the first step of Laravel launch.

Entry file

laravel is a single entry framework, and all requests must go through

define('LARAVEL_START', microtime(true)); // Get the startup time

Using composer is a logo of modern PHP

require __DIR__.'/../vendor/'; // loadcomposer -> 

Load the startup file

$app = require_once __DIR__.'/../bootstrap/';

Get$appIt is the key to laravel startup, and it can also be said that $app is the key to start the laravel kernel 🔑. Then it loads the kernel, loads the entity classes mapped by the service provider and facade, and middleware, and finally receives the http request and returns the result.

$kernel = $app->make(Illuminate\Contracts\Http\Kernel::class); // Load core class
$response = $kernel->handle(
 $request = Illuminate\Http\Request::capture()
);

$response->send();

$kernel->terminate($request, $response);

It seems like a short 4 lines of code, which is the elegance of laravel. We began to de-plane deeply.

bootstrap\

This startup file can also be regarded as a service provider, but it does not have a boot or register method. Because the entry file is loaded directly, all these unnecessary methods will not exist.

As a startup file, the home page is the loading of all necessary requirements for the framework, for example

  • registerBaseBindings
  • registerBaseServiceProviders
  • registerCoreContainerAliases,

This includes many basic methods and classes, such as

  • db [\Illuminate\Database\DatabaseManager::class]
  • auth [\Illuminate\Auth\AuthManager::class, \Illuminate\Contracts\Auth\Factory::class]
  • log [\Illuminate\Log\LogManager::class, \Psr\Log\LoggerInterface::class]
  • queue [\Illuminate\Queue\QueueManager::class, \Illuminate\Contracts\Queue\Factory::class, \Illuminate\Contracts\Queue\Monitor::class]
  • redis [\Illuminate\Redis\RedisManager::class, \Illuminate\Contracts\Redis\Factory::class]
  • etc....

The core variable of $app in the service provider is the result of Application instantiation, and the make, bind, and singleton you use in the service provider comes from its parent class Container. It is said that containers are the core concept of laravel. We will explain the concept of this in detail later.

$app = new Illuminate\Foundation\Application(
 realpath(__DIR__.'/../')
);

We have obtained the instantiation of $app, and now register the core class and exception class through $app, and return the $app to

$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
);

App\Http\Kernel

All core categories

  • System middleware
  • Group Middleware
  • Routing middleware

Of course, you need to use middleware and it is also loaded in this class, which is a file that is often used.

protected $middleware = [
   \Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class,
   \Illuminate\Foundation\Http\Middleware\ValidatePostSize::class,
   \App\Http\Middleware\TrimStrings::class,
   \Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class,
   \App\Http\Middleware\TrustProxies::class,
  ];
  
  /**
   * The application's route middleware groups.
   *
   * @var array
   */
  protected $middlewareGroups = [
   'web' => [
    \App\Http\Middleware\EncryptCookies::class,
    \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
    \Illuminate\Session\Middleware\StartSession::class,
    // \Illuminate\Session\Middleware\AuthenticateSession::class,
    \Illuminate\View\Middleware\ShareErrorsFromSession::class,
    \App\Http\Middleware\VerifyCsrfToken::class,
    \Illuminate\Routing\Middleware\SubstituteBindings::class,
   ],
   
   'api' => [
    'throttle:60,1',
    'bindings',
   ],
  ];

This core class inherits from its parent classIlluminate\Foundation\Http\Kernel::class,The core class does a lot of things, and it will store all middleware into a specified array to facilitate kernel calls and other class calls.

namespace App\Http;
 
use App\Api\Middleware\VerifyApiToken;
use Illuminate\Foundation\Http\Kernel as HttpKernel;
 
class Kernel extends HttpKernel

Back to the starting point

Laravel's startup went through a very tedious process. This is also the key point of Laravel's elegance.

$response = $kernel->handle(
 $request = Illuminate\Http\Request::capture()
);

$response->send();

$kernel->terminate($request, $response);

Passing the request in will complete the startup of the entire laravel, and as for the return of the result, the developer will return it through the controller or other accessible classes.

For more information about Laravel, readers who are interested in view the topic of this site:Laravel framework introduction and advanced tutorial》、《Summary of excellent development framework for php》、《PHP object-oriented programming tutorial》、《PHP+mysql database operation tutorial"and"Summary of common database operation techniques for php

I hope that this article will be helpful to everyone's PHP programming based on the Laravel framework.