1. 程式人生 > >Lavavel 入口檔案解讀及生命週期

Lavavel 入口檔案解讀及生命週期

這裡只貼index.php的程式碼, 深入瞭解的請訪問   
https://laravel-china.org/articles/10421/depth-mining-of-laravel-life-cycle   

<?php

/**
 * Laravel - A PHP Framework For Web Artisans
 * 一個為藝術而生的PHP框架
 * 生命週期一共分為三個階段
 *      1、 載入依賴
 *      2、 例項化
 *      3、 接收請求並響應
 * @package  Laravel
 * @author   Taylor Otwell <
[email protected]
>
*/ /* |-------------------------------------------------------------------------- | Register The Auto Loader 註冊自動載入類 |-------------------------------------------------------------------------- | | Composer provides a convenient, automatically generated class loader for | our application. We just need to utilize it! We'll simply require it | into the script here so that we don't have to worry about manual | loading any of our classes later on. It feels nice to relax. |
*/ /** * 生命週期第一階段 載入依賴 * 在__DIR__.'/../bootstrap/autoload.php'中, * 先設定程式開始時間戳, * 再使用composer/autoload_real.php::getLoader()獲取類名與類檔案的對映然後註冊元件類(根據PHP版本不同以及其他條件,分為靜態註冊和非靜態註冊) * 如果存在編譯的快取檔案,則引入 */ require __DIR__.'/../bootstrap/autoload.php'; /* |-------------------------------------------------------------------------- | Turn On The Lights |-------------------------------------------------------------------------- | | We need to illuminate PHP development, so let us turn on the lights. | This bootstraps the framework and gets it ready for use, then it | will load up this application so that we can run it and send | the responses back to the browser and delight our users. |
*/ /** * 生命週期第二階段,建立Laravel應用例項 * 建立 APP 容器、註冊應用路徑、註冊基礎服務提供者(繫結核心),並在繫結核心時 配置中介軟體和載入程式等引數 */ $app = require_once __DIR__.'/../bootstrap/app.php'; /* |-------------------------------------------------------------------------- | Run The Application |-------------------------------------------------------------------------- | | Once we have the application, we can handle the incoming request | through the kernel, and send the associated response back to | the client's browser allowing them to enjoy the creative | and wonderful application we have prepared for them. | */ /** * 宣告週期第三階段,接收請求並響應 */ $kernel = $app->make(Illuminate\Contracts\Http\Kernel::class); //使用容器$app的make()方法例項化Http核心, // 在vendor/Laravel/framework/src/Illuminate/Foundation/Http/Kernel.php檔案中 // 建構函式內將在 HTTP 核心定義的 中介軟體組 註冊到 路由器, 之後就可以呼叫 這些中介軟體 處理請求 // 建構函式接收 APP 容器 和 路由器 兩個引數 $response = $kernel->handle( $request = Illuminate\Http\Request::capture() //請求例項 Illuminate\Http\Request 的 capture() 方法內部通過 Symfony 例項建立一個 Laravel 請求例項。 //這樣我們就可以獲取到使用者請求報文的相關資訊了,以及全域性變數等資訊。 //handle() 方法接收一個 HTTP 請求, //首先,將 $request 例項註冊到 APP 容器 供後續使用; //之後,清除之前 $request 例項快取; //然後,通過$this->bootstrap();啟動「載入程式」;即我們第二階段建立容器時 配置的載入程式 bootstrappers //然後,傳送請求至路由,通過路由例項,查詢routes/web.php中路由,最終匹配到控制器或匿名函式。 //隨即進入到請求處理的流程 //並最終生成一個 HTTP 響應並返回到這裡。 ); /** * 經過一系列漫長的處理,終於來到最後 -> 傳送響應 */ $response->send(); //傳送響應由 Illuminate\Http\Response 父類 Symfony\Component\HttpFoundation\Response 中的 send() 方法完成。 $kernel->terminate($request, $response); //程式終止,完成終止中介軟體的呼叫