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)系我們修改或刪除,多謝。
新聞熱點(diǎn)
疑難解答
圖片精選