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

首頁 > 編程 > PHP > 正文

Laravel核心解讀Facades

2020-03-22 19:02:52
字體:
供稿:網(wǎng)友
這篇文章主要介紹了關(guān)于Laravel核心解讀Facades,有著一定的參考價(jià)值,現(xiàn)在分享給大家,有需要的朋友可以參考一下

什么是Facades

Facades是我們?cè)贚aravel應(yīng)用開發(fā)中使用頻率很高的一個(gè)組件,叫組件不太合適,其實(shí)它們是一組靜態(tài)類接口或者說代理,讓開發(fā)者能簡(jiǎn)單的訪問綁定到服務(wù)容器里的各種服務(wù)。Laravel文檔中對(duì)Facades的解釋如下:

Facades 為html' target='_blank'>應(yīng)用程序的 服務(wù)容器 中可用的類提供了一個(gè)「靜態(tài)」接口。Laravel 本身附帶許多的 facades,甚至你可能在不知情的狀況下已經(jīng)在使用他們!Laravel 「facades」作為在服務(wù)容器內(nèi)基類的「靜態(tài)代理」,擁有簡(jiǎn)潔、易表達(dá)的語法優(yōu)點(diǎn),同時(shí)維持著比傳統(tǒng)靜態(tài)方法更高的可測(cè)試性和靈活性。

我們經(jīng)常用的Route就是一個(gè)Facade, 它是/Illuminate/Support/Facades/Route類的別名,這個(gè)Facade類代理的是注冊(cè)到服務(wù)容器里的router服務(wù),所以通過Route類我們就能夠方便地使用router服務(wù)中提供的各種服務(wù),而其中涉及到的服務(wù)解析完全是隱式地由Laravel完成的,這在一定程度上讓應(yīng)用程序代碼變的簡(jiǎn)潔了不少。下面我們會(huì)大概看一下Facades從被注冊(cè)進(jìn)Laravel框架到被應(yīng)用程序使用這中間的流程。Facades是和ServiceProvider緊密配合的所以如果你了解了中間的這些流程對(duì)開發(fā)自定義Laravel組件會(huì)很有幫助。

注冊(cè)Facades

說到Facades注冊(cè)又要回到再介紹其它核心組建時(shí)提到過很多次的Bootstrap階段了,在讓請(qǐng)求通過中間件和路由之前有一個(gè)啟動(dòng)應(yīng)用程序的過程:

//Class: /Illuminate/Foundation/Http/Kernelprotected function sendRequestThroughRouter($request) $this- app- instance( request , $request); Facade::clearResolvedInstance( request  $this- bootstrap(); return (new Pipeline($this- app)) - send($request) - through($this- app- shouldSkipMiddleware() ? [] : $this- middleware) - then($this- dispatchToRouter());//引導(dǎo)啟動(dòng)Laravel應(yīng)用程序public function bootstrap() if (! $this- app- hasBeenBootstrapped()) { /**依次執(zhí)行$bootstrappers中每一個(gè)bootstrapper的bootstrap()函數(shù) $bootstrappers = [ Illuminate/Foundation/Bootstrap/DetectEnvironment , Illuminate/Foundation/Bootstrap/LoadConfiguration , Illuminate/Foundation/Bootstrap/ConfigureLogging , Illuminate/Foundation/Bootstrap/HandleExceptions , Illuminate/Foundation/Bootstrap/RegisterFacades , Illuminate/Foundation/Bootstrap/RegisterProviders , Illuminate/Foundation/Bootstrap/BootProviders , ];*/ $this- app- bootstrapWith($this- bootstrappers());}

在啟動(dòng)應(yīng)用的過程中Illuminate/Foundation/Bootstrap/RegisterFacades這個(gè)階段會(huì)注冊(cè)應(yīng)用程序里用到的Facades。

class RegisterFacades * Bootstrap the given application. * @param /Illuminate/Contracts/Foundation/Application $app * @return void public function bootstrap(Application $app) Facade::clearResolvedInstances(); Facade::setFacadeApplication($app); AliasLoader::getInstance(array_merge( $app- make( config )- get( app.aliases , []), $app- make(PackageManifest::class)- aliases() ))- register();}

在這里會(huì)通過AliasLoader類的實(shí)例將為所有Facades注冊(cè)別名,F(xiàn)acades和別名的對(duì)應(yīng)關(guān)系存放在config/app.php文件的$aliases數(shù)組中

 aliases = [ App = Illuminate/Support/Facades/App::class, Artisan = Illuminate/Support/Facades/Artisan::class, Auth = Illuminate/Support/Facades/Auth::class, ...... Route = Illuminate/Support/Facades/Route::class, ......]

看一下AliasLoader里是如何注冊(cè)這些別名的

// class: Illuminate/Foundation/AliasLoaderpublic static function getInstance(array $aliases = []) if (is_null(static::$instance)) { return static::$instance = new static($aliases); $aliases = array_merge(static::$instance- getAliases(), $aliases); static::$instance- setAliases($aliases); return static::$instance;public function register() if (! $this- registered) { $this- prependToLoaderStack(); $this- registered = true;protected function prependToLoaderStack() // 把AliasLoader::load()放入自動(dòng)加載函數(shù)隊(duì)列中,并置于隊(duì)列頭部 spl_autoload_register([$this, load ], true, true);}

通過上面的代碼段可以看到AliasLoader將load方法注冊(cè)到了SPL __autoload函數(shù)隊(duì)列的頭部??匆幌耹oad方法的源碼:

public function load($alias) if (isset($this- aliases[$alias])) { return class_alias($this- aliases[$alias], $alias);}

在load方法里$aliases配置里的Facade類創(chuàng)建了對(duì)應(yīng)的別名,比如當(dāng)我們使用別名類Route時(shí)PHP會(huì)通過AliasLoader的load方法為把Illuminate/Support/Facades/Route::class類創(chuàng)建一個(gè)別名類Route,所以我們?cè)诔绦蚶锸褂脛eRoute其實(shí)使用的就是`Illuminate/Support/Facades/Route類。

解析Facade代理的服務(wù)

把Facades注冊(cè)到框架后我們?cè)趹?yīng)用程序里就能使用其中的Facade了,比如注冊(cè)路由時(shí)我們經(jīng)常用Route::get( /uri , Controller@action);,那么Route是怎么代理到路由服務(wù)的呢,這就涉及到在Facade里服務(wù)的隱式解析了, 我們看一下Route類的源碼:

鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請(qǐng)第一時(shí)間聯(lián)系我們修改或刪除,多謝。

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 会理县| 雷山县| 年辖:市辖区| 松滋市| 布尔津县| 易门县| 玉溪市| 伊宁县| 巴彦县| 安化县| 颍上县| 东至县| 河源市| 庆云县| 仪征市| 麟游县| 扎兰屯市| 方山县| 双流县| 三河市| 和平县| 吐鲁番市| 肇庆市| 满城县| 神农架林区| 新昌县| 英山县| 长寿区| 炎陵县| 越西县| 湟中县| 鲜城| 米泉市| 禄丰县| 凤台县| 千阳县| 白水县| 九龙坡区| 凉山| 鄯善县| 和林格尔县|