We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
App\Http\Kernel 继承自 Illuminate\Foundation\Http\Kernel 类,所以本文章的分析主要集中在 app/Http/Kernel.php 和 vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php 两个类中
App\Http\Kernel
Illuminate\Foundation\Http\Kernel
因为 App\Http\Kernel 没有 __construct 方法,所以穿透到了 Illuminate\Foundation\Http\Kernel 的 __construct:
laravel/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php
Lines 82 to 103 in d081c91
首先是此方法传入参数的 __construct(Application $app, Router $router) 声明,这里恰好使用了 Laravel 容器的依赖注入(Dependency Injection,Inversion of Control的一种)设计。具体的在本篇不讲述,可参见本篇末尾的单独分析的链接的文章。
__construct(Application $app, Router $router)
执行到 __construct 时, Illuminate\Foundation\Application 容器会将 Illuminate\Foundation\Application(即应用容器自身)和 Illuminate\Routing\Router 注入到方法内。然后逻辑代码将两个对象赋给 App\Http\Kernel 的 $this 属性中。
Illuminate\Foundation\Application
Illuminate\Routing\Router
$this
然后将 Illuminate\Foundation\Http\Kernel 的 $middlewarePriority 属性
Lines 66 to 80 in d081c91
根据其注释,此属性是强制对 middleward 中间件执行顺序进行排序的作用。
在后面将 App\Http\Kernel 中声明的 $middlewareGroup
$middlewareGroup
laravel/app/Http/Kernel.php
Lines 24 to 44 in d081c91
laravel/vendor/laravel/framework/src/Illuminate/Routing/Router.php
Lines 854 to 866 in d081c91
紧接着,将
Lines 46 to 63 in d081c91
Lines 819 to 831 in d081c91
至此, Kernel::__construct() 解析完毕。
Kernel::__construct()
Lines 105 to 132 in d081c91
Symfony\Component\HttpFoundation\Request
Illuminate\Http\Request 继承自 Symfony\Component\HttpFoundation\Request 并且 Illuminate\Http\Request 未覆盖 enableHttpMethodParameterOverride
Illuminate\Http\Request
laravel/vendor/symfony/http-foundation/Request.php
Lines 638 to 652 in d081c91
然后,调用 Illuminate\Foundation\Http\Kernel 的 sendRequestThroughRouter
Lines 134 to 152 in d081c91
通过第142行,将 $request 注入进 Illuminate\Foundation\Application 容器。
144行,将门面类中的 request 数据清理掉
request
laravel/vendor/laravel/framework/src/Illuminate/Support/Facades/Facade.php
Lines 164 to 173 in d081c91
然后146行,调用 Illuminate\Foundation\Http\Kernel 的 bootstrap 方法
Lines 154 to 164 in d081c91
第161执行到容器的 hasBeenBootstrapped 方法
laravel/vendor/laravel/framework/src/Illuminate/Foundation/Application.php
Lines 249 to 257 in d081c91
第162实际得到这个数组
Lines 31 to 43 in d081c91
bootstrapWith
Lines 193 to 210 in d081c91
特别留意206行的 make 调用
make
关于容器 make 方法的细节 请查阅 10. 容器的 singleton 和 bind 的实现 的 “揭开 Container::make() 神秘的面纱” 段落
Lines 716 to 734 in d081c91
Lines 691 to 714 in d081c91
boot()
$this->fireAppCallbacks($this->bootingCallbacks)
然后结果就是依次执行
\Illuminate\Foundation\Bootstrap\LoadEnvironmentVariables
\Illuminate\Foundation\Bootstrap\LoadConfiguration
\Illuminate\Foundation\Bootstrap\HandleExceptions
\Illuminate\Foundation\Bootstrap\RegisterFacades
\Illuminate\Foundation\Bootstrap\RegisterProviders
\Illuminate\Foundation\Bootstrap\BootProviders
这些 Bootstrap 类的 bootstrap 方法
laravel/vendor/laravel/framework/src/Illuminate/Foundation/Bootstrap/LoadEnvironmentVariables.php
Lines 13 to 35 in d081c91
laravel/vendor/laravel/framework/src/Illuminate/Foundation/Bootstrap/LoadConfiguration.php
Lines 14 to 52 in d081c91
laravel/vendor/laravel/framework/src/Illuminate/Foundation/Bootstrap/HandleExceptions.php
Lines 22 to 43 in d081c91
laravel/vendor/laravel/framework/src/Illuminate/Foundation/Bootstrap/RegisterFacades.php
Lines 12 to 28 in d081c91
laravel/vendor/laravel/framework/src/Illuminate/Foundation/Bootstrap/RegisterProviders.php
Lines 9 to 18 in d081c91
laravel/vendor/laravel/framework/src/Illuminate/Foundation/Bootstrap/BootProviders.php
上面列出的清单中,分别作用为
其中最后两项在 laravel 请求的生命周期中是至关重要的,我们将在后续文章中重点讲解。
在 bootstrap 阶段结束后,Kernel::sendRequestThroughRouter 后面带 pipeline 关键字的代码就是管道。
bootstrap
Kernel::sendRequestThroughRouter
pipeline
关于管道请查阅 05. Pipeline 解析
Lines 148 to 151 in d081c91
在进入管道前, 调用了 dispatchToRouter 返回一个闭包对象
dispatchToRouter
Lines 166 to 178 in d081c91
匹配路由的逻辑清晰可见
Lines 601 to 682 in d081c91
如果一路抽丝剥茧,我们便能找到 Router 调用 controller 的逻辑了。 请见06. RouteServiceProvider 详解 最后段落。
The text was updated successfully, but these errors were encountered:
No branches or pull requests
Kernel Handle
App\Http\Kernel
继承自Illuminate\Foundation\Http\Kernel
类,所以本文章的分析主要集中在 app/Http/Kernel.php 和 vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php 两个类中__construct 解析
因为
App\Http\Kernel
没有 __construct 方法,所以穿透到了Illuminate\Foundation\Http\Kernel
的 __construct:laravel/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php
Lines 82 to 103 in d081c91
首先是此方法传入参数的
__construct(Application $app, Router $router)
声明,这里恰好使用了 Laravel 容器的依赖注入(Dependency Injection,Inversion of Control的一种)设计。具体的在本篇不讲述,可参见本篇末尾的单独分析的链接的文章。执行到 __construct 时,
Illuminate\Foundation\Application
容器会将Illuminate\Foundation\Application
(即应用容器自身)和Illuminate\Routing\Router
注入到方法内。然后逻辑代码将两个对象赋给App\Http\Kernel
的$this
属性中。然后将
Illuminate\Foundation\Http\Kernel
的 $middlewarePriority 属性laravel/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php
Lines 66 to 80 in d081c91
赋值给
Illuminate\Routing\Router
的 $middlewarePriority 属性根据其注释,此属性是强制对 middleward 中间件执行顺序进行排序的作用。
在后面将
App\Http\Kernel
中声明的$middlewareGroup
laravel/app/Http/Kernel.php
Lines 24 to 44 in d081c91
在
Illuminate\Foundation\Http\Kernel
的 96-98行,调用Illuminate\Routing\Router
的 middlewareGroup 方法,存到 $router 的 $middlewareGroup 中laravel/vendor/laravel/framework/src/Illuminate/Routing/Router.php
Lines 854 to 866 in d081c91
紧接着,将
laravel/app/Http/Kernel.php
Lines 46 to 63 in d081c91
的中间件,调用
Illuminate\Routing\Router
的 aliasMiddleware 的方法,绑定到 $router 的 $middleware 中laravel/vendor/laravel/framework/src/Illuminate/Routing/Router.php
Lines 819 to 831 in d081c91
至此,
Kernel::__construct()
解析完毕。handle 解析
laravel/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php
Lines 105 to 132 in d081c91
调用到
Symfony\Component\HttpFoundation\Request
的 enableHttpMethodParameterOverride 方法laravel/vendor/symfony/http-foundation/Request.php
Lines 638 to 652 in d081c91
然后,调用
Illuminate\Foundation\Http\Kernel
的 sendRequestThroughRouterlaravel/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php
Lines 134 to 152 in d081c91
通过第142行,将 $request 注入进
Illuminate\Foundation\Application
容器。144行,将门面类中的
request
数据清理掉laravel/vendor/laravel/framework/src/Illuminate/Support/Facades/Facade.php
Lines 164 to 173 in d081c91
Bootstrap 解析
然后146行,调用
Illuminate\Foundation\Http\Kernel
的 bootstrap 方法laravel/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php
Lines 154 to 164 in d081c91
第161执行到容器的 hasBeenBootstrapped 方法
laravel/vendor/laravel/framework/src/Illuminate/Foundation/Application.php
Lines 249 to 257 in d081c91
第162实际得到这个数组
laravel/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php
Lines 31 to 43 in d081c91
然后将数组做为参数,执行容器的
bootstrapWith
方法laravel/vendor/laravel/framework/src/Illuminate/Foundation/Application.php
Lines 193 to 210 in d081c91
特别留意206行的
make
调用laravel/vendor/laravel/framework/src/Illuminate/Foundation/Application.php
Lines 716 to 734 in d081c91
关于 loadDeferredProvider 的逻辑会最终执行到
laravel/vendor/laravel/framework/src/Illuminate/Foundation/Application.php
Lines 691 to 714 in d081c91
第710行的 booting 方法,是登记一个闭包 (并不会马上执行这个闭包), 然后这个服务提供者在
boot()
阶段的$this->fireAppCallbacks($this->bootingCallbacks)
才会真正被创建。 关联阅读请见 04. ServiceProvider Boot 解析然后结果就是依次执行
\Illuminate\Foundation\Bootstrap\LoadEnvironmentVariables
\Illuminate\Foundation\Bootstrap\LoadConfiguration
\Illuminate\Foundation\Bootstrap\HandleExceptions
\Illuminate\Foundation\Bootstrap\RegisterFacades
\Illuminate\Foundation\Bootstrap\RegisterProviders
\Illuminate\Foundation\Bootstrap\BootProviders
这些 Bootstrap 类的 bootstrap 方法
laravel/vendor/laravel/framework/src/Illuminate/Foundation/Bootstrap/LoadEnvironmentVariables.php
Lines 13 to 35 in d081c91
laravel/vendor/laravel/framework/src/Illuminate/Foundation/Bootstrap/LoadConfiguration.php
Lines 14 to 52 in d081c91
laravel/vendor/laravel/framework/src/Illuminate/Foundation/Bootstrap/HandleExceptions.php
Lines 22 to 43 in d081c91
laravel/vendor/laravel/framework/src/Illuminate/Foundation/Bootstrap/RegisterFacades.php
Lines 12 to 28 in d081c91
laravel/vendor/laravel/framework/src/Illuminate/Foundation/Bootstrap/RegisterProviders.php
Lines 9 to 18 in d081c91
laravel/vendor/laravel/framework/src/Illuminate/Foundation/Bootstrap/BootProviders.php
Lines 9 to 18 in d081c91
上面列出的清单中,分别作用为
\Illuminate\Foundation\Bootstrap\LoadEnvironmentVariables
\Illuminate\Foundation\Bootstrap\LoadConfiguration
\Illuminate\Foundation\Bootstrap\HandleExceptions
\Illuminate\Foundation\Bootstrap\RegisterFacades
\Illuminate\Foundation\Bootstrap\RegisterProviders
\Illuminate\Foundation\Bootstrap\BootProviders
其中最后两项在 laravel 请求的生命周期中是至关重要的,我们将在后续文章中重点讲解。
在
bootstrap
阶段结束后,Kernel::sendRequestThroughRouter
后面带pipeline
关键字的代码就是管道。laravel/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php
Lines 148 to 151 in d081c91
在进入管道前, 调用了
dispatchToRouter
返回一个闭包对象laravel/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php
Lines 166 to 178 in d081c91
匹配路由的逻辑清晰可见
laravel/vendor/laravel/framework/src/Illuminate/Routing/Router.php
Lines 601 to 682 in d081c91
The text was updated successfully, but these errors were encountered: