根据以往的入门经验,最快入门的方法就是直接去读框架的源代码。
启动流程
首先进入网站的运行目录(/public/)
index.php定义了APP_PATH 也就是用户的应用目录,然后加载tp框架中的start.php
1 2
| define('APP_PATH', __DIR__ . '/../application/'); require __DIR__ . '/../thinkphp/start.php';
|
start.php 又引用了 base.php。
base.php 做了以下三件事
1.定义了一堆目录变量和环境变量
2.引用loader类,注册了自动加载函数 \think\Loader::autoload,同时执行了Composer自动加载
3.注册了Exception异常处理类
3.加载了核心配置文件 convention.php
重点在2,Composer通过/vendor/composer/autoload_files.php引入了以下几个类库。
\think\Route
\think\Config
\think\Validate
\think\Console
\think\Error
然后start.php 执行了 \App::run()->send();
首先来看下 App::run()
1 2 3 4
| public static function run(Request $request = null){ is_null($request) && $request = Request::instance(); ... }
|
run方法引入了 类型约束,指定$request为Request类的对象,第一行又做了一次判断,如果$request为空时,实例化一次Request类。
run方法做了什么?
- 使用initCommon()初始化了应用的方法
- 调用init()读取用户应用的一些配置信息
- 设置了全局请求参数过滤方法
- 设置了默认的语言包
- routeCheck()路由检测
- 日志记录
- HOOK url缓存等等
- 根据路由 执行对应规则的方法(此处会返回对应的响应结果)
- 把 结果 压入\think\Response,并通过Response->send()方法输出
关于 start.php 中 App::run()->send(); 此处的send()方法并不是App类中的。
因为在App::run()中有以下几行代码
此处已经把$response实例化为Response对象,之后执行 Response->send()
1 2 3 4 5 6 7 8 9 10 11 12
| if ($data instanceof Response) { $response = $data; } elseif (!is_null($data)) { $isAjax = $request->isAjax(); $type = $isAjax ? Config::get('default_ajax_return') : Config::get('default_return_type'); $response = Response::create($data, $type); } else { $response = Response::create(); } return $response;
|