### **立刻馬上安裝ThinkPHP6 打開 public/index.php開始源碼攻讀**
首先了解一下框架的加載流程做到心中有全局。
對于一個HTTP應用來說,從用戶發起請求到響應輸出結束,大致的標準請求流程如下:
載入Composer的自動加載autoload文件
require __DIR__ . '/../vendor/autoload.php';
實例化系統應用基礎類think\App
$http = (new App());
獲取應用目錄等相關路徑信息
$this->thinkPath = dirname(__DIR__) . DIRECTORY_SEPARATOR;
$this->rootPath = $rootPath ? rtrim($rootPath, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR : $this->getDefaultRootPath();
$this->appPath = $this->rootPath . 'app' . DIRECTORY_SEPARATOR;
$this->runtimePath = $this->rootPath . 'runtime' . DIRECTORY_SEPARATOR;
加載全局的服務提供provider.php文件
if (is_file($this->appPath . 'provider.php')) {
$this->bind(include $this->appPath . 'provider.php');
}
設置容器實例及應用對象實例,確保當前容器對象唯一
$this->instance('app', $this);
$this->instance('think\Container', $this);
從容器中獲取HTTP應用類think\Http
$http = (new App())->http;
執行HTTP應用類的run方法啟動一個HTTP應用
$response = $http->run();
獲取當前請求對象實例(默認為 app\Request 繼承think\Request)保存到容器
//自動創建request對象
$request = $request ?? $this->app->make('request', [], true);
$this->app->instance('request', $request);
執行think\App類的初始化方法initialize
$this->initialize();
加載環境變量文件.env和全局初始化文件
加載全局公共文件、系統助手函數、全局配置文件、全局事件定義和全局服務定義
判斷應用模式(調試或者部署模式)
監聽AppInit事件
?注冊異常處理
服務注冊
啟動注冊的服務
加載全局中間件定義
// 加載全局中間件
$this->loadMiddleware();
監聽HttpRun事件
// 設置開啟事件機制
$this->app->event->withEvent($this->app->config->get('app.with_event', true));
// 監聽HttpRun
$this->app->event->trigger(HttpRun::class);
執行全局中間件
return $this->app->middleware->pipeline()
->send($request)
->then(function ($request) {
return $this->dispatchToRoute($request);
});
執行路由調度(Route類dispatch方法)
protected function dispatchToRoute($request)
{
$withRoute = $this->app->config->get('app.with_route', true) ? function () {
$this->loadRoutes();
} : null;
return $this->app->route->dispatch($request, $withRoute);
}
如果開啟路由則檢查路由緩存
加載路由定義
監聽RouteLoaded事件
$routePath = $this->getRoutePath();
if (is_dir($routePath)) {
$files = glob($routePath . '*.php');
foreach ($files as $file) {
include $file;
}
}
$this->app->event->trigger(RouteLoaded::class);
?如果開啟注解路由則檢測注解路由
?路由檢測(中間流程很復雜 略)
路由調度對象think\route\Dispatch初始化
// 獲取控制器名
$controller = strip_tags($result[0] ?: $this->rule->config('default_controller'));
if (strpos($controller, '.')) {
$pos = strrpos($controller, '.');
$this->controller = substr($controller, 0, $pos) . '.' . Str::studly(substr($controller, $pos + 1));
} else {
$this->controller = Str::studly($controller);
}
設置當前請求的控制器和操作名
// 獲取操作名
$this->actionName = strip_tags($result[1] ?: $this->rule->config('default_action'));
注冊路由中間件
if (!empty($this->config['middleware'])) {
$this->app->middleware->import($this->config['middleware'], 'route');
}
綁定數據模型
設置路由額外參數
$this->group->removeSlash($this->removeSlash);
執行數據自動驗證
if ($this->rule instanceof RuleItem && $this->request->method() == 'OPTIONS' && $this->rule->isAutoOptions()) {
$rules = $this->rule->getRouter()->getRule($this->rule->getRule());
$allow = [];
foreach ($rules as $item) {
$allow[] = strtoupper($item->getMethod());
}
return Response::create('', 'html', 204)->header(['Allow' => implode(', ', $allow)]);
}
執行路由調度子類的exec方法返回響應think\Response對象
$data = $this->exec();
return $this->autoResponse($data);
獲取當前請求的控制器對象實例
利用反射機制注冊控制器中間件
執行控制器方法以及前后置中間件
執行當前響應對象的send方法輸出
$response->send();
執行HTTP應用對象的end方法善后
$http->end($response);
監聽HttpEnd事件
執行中間件的end回調
寫入當前請求的日志信息