Laravel - Boot

时间:2023-03-12 17:00:00

本文章为 Laravel 的引导过程,参考的文档版本:Laravel 10。


  1. 第一步:进入入口 /public/index.php

如果是维护模式,就引入:/storage/framework/maintenance.php

引入系统依赖自动加载文件:/vendor/autoload.php

引入用户自定义的引导文件:/bootstrap/app.php


  1. 第二步:实例化内核 一般是 ​​Http Kernel 或 Console Kernel​​。

Laravel 会自动实例化其父类中的 bootstrappers 数组中声明的类,这些类如下:

(1)​​Http Kernel​​,文件:/app/Http/Kernel.php ,父文件中引导类声明的数组为:

protected $bootstrappers = [
\Illuminate\Foundation\Bootstrap\LoadEnvironmentVariables::class,
\Illuminate\Foundation\Bootstrap\LoadConfiguration::class,
\Illuminate\Foundation\Bootstrap\HandleExceptions::class,
\Illuminate\Foundation\Bootstrap\RegisterFacades::class,
\Illuminate\Foundation\Bootstrap\RegisterProviders::class,
\Illuminate\Foundation\Bootstrap\BootProviders::class,
];

(2)​​Console Kernel​​,文件:/app/Console/Kernel.php,父文件中引导类声明的数组为:

protected $bootstrappers = [
\Illuminate\Foundation\Bootstrap\LoadEnvironmentVariables::class,
\Illuminate\Foundation\Bootstrap\LoadConfiguration::class,
\Illuminate\Foundation\Bootstrap\HandleExceptions::class,
\Illuminate\Foundation\Bootstrap\RegisterFacades::class,
\Illuminate\Foundation\Bootstrap\SetRequestForConsole::class,
\Illuminate\Foundation\Bootstrap\RegisterProviders::class,
\Illuminate\Foundation\Bootstrap\BootProviders::class,
];

在内核中实例化基础服务: ​​环境、配置、异常、门面、注册服务提供者引导类、注册引导提供者​​等。


  1. 第三步:实例化服务提供者

这一步,将会实例化 /config/app.php 中,所有 ​​providers​​​ 下面注册的内容提供者,调用它们的 ​​boot 方法​

'providers' => [
Illuminate\Auth\AuthServiceProvider::class,
Illuminate\Broadcasting\BroadcastServiceProvider::class,
Illuminate\Bus\BusServiceProvider::class,
Illuminate\Cache\CacheServiceProvider::class,
Illuminate\Foundation\Providers\ConsoleSupportServiceProvider::class,
Illuminate\Cookie\CookieServiceProvider::class,
Illuminate\Database\DatabaseServiceProvider::class,
Illuminate\Encryption\EncryptionServiceProvider::class,
Illuminate\Filesystem\FilesystemServiceProvider::class,
Illuminate\Foundation\Providers\FoundationServiceProvider::class,
Illuminate\Hashing\HashServiceProvider::class,
Illuminate\Mail\MailServiceProvider::class,
Illuminate\Notifications\NotificationServiceProvider::class,
Illuminate\Pagination\PaginationServiceProvider::class,
Illuminate\Pipeline\PipelineServiceProvider::class,
Illuminate\Queue\QueueServiceProvider::class,
Illuminate\Redis\RedisServiceProvider::class,
Illuminate\Auth\Passwords\PasswordResetServiceProvider::class,
Illuminate\Session\SessionServiceProvider::class,
Illuminate\Translation\TranslationServiceProvider::class,
Illuminate\Validation\ValidationServiceProvider::class,
Illuminate\View\ViewServiceProvider::class,

App\Providers\AppServiceProvider::class,
App\Providers\AuthServiceProvider::class,
// App\Providers\BroadcastServiceProvider::class,
App\Providers\EventServiceProvider::class,
App\Providers\RouteServiceProvider::class,
],

实例话一般性服务:​​路由、权限、会话、验证器、数据库、文件、缓存、加解密、转换器、通知、队列、视图服务​​ 等。


  1. 第四步:解析路由

路由服务提供者:路由器解析 路由并调用一系列 中间件 后,交给 控制器 去执行。


  1. 第五步:返回响应

当在路由器,或者控制器中返回响应之后,index.php 中的 kernel 将会调用 ​​send​​ 方法将其发送,请求周期完成。