圖解Laravel的生命周期


先來張圖大致理解下laravel的生命周期。

下面對應相應的代碼,解釋上圖。

//文件路徑:laravel/public/index.php

/**
 * laravel的啟動時間
 */
define('LARAVEL_START', microtime(true));

/**
 * 加載項目依賴。
 * 現代PHP依賴於Composer包管理器,入口文件通過引入由Composer包管理器。
 * 自動生成的類加載程序,可以輕松注冊並加載所依賴的第三方組件庫。
 */
require __DIR__.'/../vendor/autoload.php';

/**
 * 創建laravel應用實例。
 */
$app = require_once __DIR__.'/../bootstrap/app.php';

// 接受請求並響應
$kernel = $app->make(Illuminate\Contracts\Http\Kernel::class);

$response = $kernel->handle(
    $request = Illuminate\Http\Request::capture()
);

// 結束請求,進行回調
$response->send();

// 終止程序
$kernel->terminate($request, $response);
//文件路徑:laravel/boostrap/app.php

//第一部分:創建應用實例
$app = new Illuminate\Foundation\Application(
    realpath(__DIR__.'/../')
);

//第二部分:完成內核綁定
$app->singleton(
    Illuminate\Contracts\Http\Kernel::class,
    App\Http\Kernel::class
);

$app->singleton(
    Illuminate\Contracts\Console\Kernel::class,
    App\Console\Kernel::class
);

$app->singleton(
    Illuminate\Contracts\Debug\ExceptionHandler::class,
    App\Exceptions\Handler::class
);

return $app;
文件路徑:laravel\vendor\laravel\framework\src\Illuminate\Foundation\Http\Kernel.php

class Kernel implements KernelContract
{
    protected $bootstrappers = [
        \Illuminate\Foundation\Bootstrap\LoadEnvironmentVariables::class, // 注冊系統環境配置
        \Illuminate\Foundation\Bootstrap\LoadConfiguration::class,              // 注冊系統配置 
        \Illuminate\Foundation\Bootstrap\HandleExceptions::class,              // 注冊異常注冊
        \Illuminate\Foundation\Bootstrap\RegisterFacades::class,                // 注冊門面模式
        \Illuminate\Foundation\Bootstrap\RegisterProviders::class,              // 注冊服務提供者 
        \Illuminate\Foundation\Bootstrap\BootProviders::class,                    // 注冊服務提供者boot
    ];

    // 處理請求
    public function handle($request)
    {
        try {
            $request->enableHttpMethodParameterOverride();

            $response = $this->sendRequestThroughRouter($request);
        } catch (Exception $e) {
            $this->reportException($e);

            $response = $this->renderException($request, $e);
        } catch (Throwable $e) {
            $this->reportException($e = new FatalThrowableError($e));

            $response = $this->renderException($request, $e);
        }

        $this->app['events']->dispatch(
            new Events\RequestHandled($request, $response)
        );

        return $response;
    }

    protected function sendRequestThroughRouter($request)
    {
        //一、將$request實例注冊到APP容器
        $this->app->instance('request', $request);

        //二、清除之前的$request實例緩存
        Facade::clearResolvedInstance('request');

        //三、啟動引導程序
        $this->bootstrap();

        //四、發送請求
        return (new Pipeline($this->app)) //創建管道
                    ->send($request)      //發送請求
                    ->through($this->app->shouldSkipMiddleware() ? [] : $this->middleware)  //通過中間件
                    ->then($this->dispatchToRouter());  //分發到路由
    }

    // 啟動引導程序
    public function bootstrap()
    {
        if (! $this->app->hasBeenBootstrapped()) {
            $this->app->bootstrapWith($this->bootstrappers());
        }
    }
    
    // 路由分發
    protected function dispatchToRouter()
    {
        return function ($request) {
            $this->app->instance('request', $request);

            return $this->router->dispatch($request);
        };
    }

    // 終止程序
    public function terminate($request, $response)
    {
        $this->terminateMiddleware($request, $response);

        $this->app->terminate();
    }


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM