Laravel 5.x 启动过程分析 [转]
2024-10-15 03:25:34
1、初始化Application
1.1 注册基本绑定
- app -> Application实例(Illuminate\Foundation\Application)
- Illuminate\Container\Container -> Application实例(Illuminate\Foundation\Application)
1.2 注册基本服务提供者并启动
EventServieProvider —— 事件服务提供者
$this->app->singleton('events', function ($app) {
return (new Dispatcher($app))->setQueueResolver(function () use ($app) {
return $app->make('Illuminate\Contracts\Queue\Factory');
});
});
RoutingServiceProvider —— 路由服务提供者
public function register()
{
$this->registerRouter();
$this->registerUrlGenerator();
$this->registerRedirector();
$this->registerPsrRequest();
$this->registerPsrResponse();
$this->registerResponseFactory();
}
更多详情查看源码:Illuminate\Routing\RoutingServiceProvider.php
1.3 注册核心服务容器别名
更多详情查看源码:Illuminate\Foundation\Application.php
第1026行registerCoreContainerAliases
方法。
1.4 设置根路径(如果传入的话)
if ($basePath) {
$this->setBasePath($basePath);
}
更多详情查看源码:Illuminate\Foundation\Application.php
第262行setBasePath
方法。
2、注册共享的Kernel和异常处理器
- Illuminate\Contracts\Http\Kernel -> App\Http\Kernel
- Illuminate\Contracts\Console\Kernel -> App\Console\Kernel
- Illuminate\Contracts\Debug\ExceptionHandler -> App\Exceptions\Handler
3、处理请求和响应
3.1 web请求
解析Illuminate\Contracts\Http\Kernel,实例化App\Http\Kernel
a.构造函数:设置$app/$router,初始化$router中middleware数值
b.handle处理请求 —— 经过路由发送请求:
- $request是经过Symfony封装的请求对象
- 注册request实例到容器 ($app[‘request’]->Illuminate\Http\Request)
- 清空之前容器中的request实例
- 调用
bootstrap
方法,启动一系列启动类的bootstrap方法:- Illuminate\Foundation\Bootstrap\DetectEnvironment 环境配置($app[‘env’])
- Illuminate\Foundation\Bootstrap\LoadConfiguration 基本配置($app[‘config’])
- Illuminate\Foundation\Bootstrap\ConfigureLogging 日志文件($app[‘log’])
- Illuminate\Foundation\Bootstrap\HandleExceptions 错误&异常处理
- Illuminate\Foundation\Bootstrap\RegisterFacades 清除已解析的Facade并重新启动,注册config文件中alias定义的所有Facade类到容器
- Illuminate\Foundation\Bootstrap\RegisterProviders 注册config中providers定义的所有Providers类到容器
- Illuminate\Foundation\Bootstrap\BootProviders 调用所有已注册Providers的boot方法
- 通过
Pipeline
发送请求,经过中间件,再由路由转发,最终返回响应new Pipeline($this->app))
->send($request)
->through($this->middleware)
->then($this->dispatchToRouter()
c.将响应信息发送到浏览器:
$response->send();
d.处理继承自TerminableMiddleware
接口的中间件(Session)并结束应用生命周期:
$kernel->terminate($request, $response);
最新文章
- 单独使用Mybatis的配置文件
- JavaScript系列文章:详解正则表达式之一
- 由STL所想到的 C++显示调用析构函数
- 从range和xrange的性能对比到yield关键字(上)
- CSS3之渐变Gradient
- c++在函数后面加const
- Eclipse用法和技巧四:生成说明文档1
- AnimatorController动画遮罩
- 在服务器上实现SSH(Single Stage Headless)
- python3自带工具2to3.py用法
- ActiveMQ(2)---ActiveMQ原理分析之消息发送
- SolrCloud集群配置
- python 的文件操作
- HGOI20180814 (NOIP 模拟Day1)
- jsp如何往js里传值
- 开源第三方登录组件OAuthLogin2.0 架构解析及开源地址
- 机器学习之路:python 多项式特征生成PolynomialFeatures 欠拟合与过拟合
- vue遇见better-scroll
- Makefile自动编写工具实例
- poj 2356 抽屉原理