国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

laravel5.1 源碼閱讀

2019-11-11 05:17:57
字體:
供稿:網(wǎng)友

laravel大致的運(yùn)行過程,先記錄一下。 當(dāng)然我是在看到了 這個(gè)博文https://my.oschina.net/falcon10086/blog/647507,寫的很棒。

laravel唯一的路口文件 /public/index.php 一步一步來看

require __DIR__.'/../bootstrap/autoload.php';

引入類加載器,查看autoload.php文件

define('LARAVEL_START', microtime(true));require __DIR__.'/../vendor/autoload.php';$compiledPath = __DIR__.'/cache/compiled.php';if (file_exists($compiledPath)) { require $compiledPath;}

第一行代碼定義程序運(yùn)行的開始事件; 第二段代碼請(qǐng)求了vendor/autoload.php文件,也就是說使用了composer自帶的類加載器。 第三段和第四段代碼用來優(yōu)化效率。

現(xiàn)在繼續(xù)看index.php文件

$app = require_once __DIR__.'/../bootstrap/app.php';

查看bootstrap/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;

先創(chuàng)建了一個(gè) Illuminate/Foundation/Application 實(shí)例,該類繼承Illuminate/Container/Container,調(diào)用其singleton()方法綁定接口和類,最后返回實(shí)例,這里是一個(gè)單例。

看一下 Illuminate/Foundation/Application 構(gòu)造函數(shù)

public function __construct($basePath = null) { $this->registerBaseBindings(); $this->registerBaseServicePRoviders(); $this->registerCoreContainerAliases(); if ($basePath) { $this->setBasePath($basePath); } }

$this->registerBaseBindings()方法

protected function registerBaseBindings() { /* * 首先需要明白當(dāng)前類繼承自 Illuminate/Container/Container * * setInstance 是 Illuminate/Container/Container 中的方法將類和當(dāng)前對(duì)象綁定, 當(dāng)然這樣說是為了跟容易表示,下面的注釋也是如此 */ static::setInstance($this); /* * 將'app'和當(dāng)前對(duì)象放在 Illuminate/Container/Container 的 instances 數(shù)組中, * 形成一個(gè)映射,當(dāng)然當(dāng)前類繼承自 Illuminate/Container/Container 也就是調(diào)用自己的 instance() 方法 * 這樣'app'指向了當(dāng)前對(duì)象,實(shí)現(xiàn)綁定。 */ $this->instance('app', $this); /* * 和上面的類似,綁定 */ $this->instance('Illuminate/Container/Container', $this); }

$this->registerBaseServiceProviders()

protected function registerBaseServiceProviders() { /* * 依賴注入: 當(dāng)前對(duì)象被注入到 EventServiceProvider、RoutingServiceProvider 中 */ $this->register(new EventServiceProvider($this)); $this->register(new RoutingServiceProvider($this)); }

注冊(cè)服務(wù)提供器。

$this->registerCoreContainerAliases()

public function registerCoreContainerAliases() { $aliases = [ 'app' => ['Illuminate/Foundation/Application', 'Illuminate/Contracts/Container/Container', 'Illuminate/Contracts/Foundation/Application'] ... ]; foreach ($aliases as $key => $aliases) { foreach ((array) $aliases as $alias) { $this->alias($key, $alias); } } }

注冊(cè)核心類的別名,簡(jiǎn)化命名; aliases 數(shù)組中一個(gè)復(fù)雜的名字對(duì)應(yīng)別名。 例如下面的: Illuminate/Foundation/Application => app; Illuminate/Contracts/Container/Container => app; foreach 就是具體的綁定。

現(xiàn)在分析app.php中的$app->singleton()。該方法將接口和具體實(shí)現(xiàn)類綁定在一起,之后用到接口的時(shí)候會(huì)調(diào)用對(duì)應(yīng)的具體實(shí)現(xiàn)。該部分源碼比較復(fù)雜,可以自己看一下,主要部分是 Illuminate/Container/Container 的 make 和 build 方法。

現(xiàn)在再次回到index.php

$kernel = $app->make(Illuminate/Contracts/Http/Kernel::class);

由于 bootstrap/app.php 中綁定了接口和具體實(shí)現(xiàn),這里調(diào)用 make 就會(huì)創(chuàng)建App/Http/Kernel::class 類實(shí)例。

最后kernel 處理 http 請(qǐng)求,接收請(qǐng)求, 發(fā)送響應(yīng), 終止程序,處理一些收尾的工作。


發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 枞阳县| 华池县| 杭锦后旗| 关岭| 青州市| 清河县| 明光市| 和平区| 印江| 社旗县| 阜南县| 兰坪| 胶州市| 凌源市| 精河县| 新化县| 宜章县| 广平县| 湖南省| 吉隆县| 哈尔滨市| 东乡族自治县| 奉化市| 正安县| 东光县| 霍州市| 如东县| 祁门县| 普兰店市| 辽宁省| 长宁区| 元江| 阿合奇县| 疏勒县| 隆林| 榕江县| 乐安县| 玉树县| 磴口县| 宣城市| 宣威市|