摘要:而這些通用的接口已經(jīng)由為你設計好了。作為在服務容器內(nèi)基類的靜態(tài)代理。所有對的調(diào)用都會被轉發(fā)到該類對象下。在注冊類在注冊自定義的別名使用測試去查看輸出
Contracts
Contracts其實就是倡導面向接口編程,來達到解耦的目的。而這些通用的接口已經(jīng)由Laravel為你設計好了。就是這些Contracts.
那么Laravel如何知道我們需要使用哪個實現(xiàn)呢?
在Laravel默認的Contracts綁定中,在"Illuminate/Foundation/Application.php"有這樣的定義:這就是綁定了默認的接口實現(xiàn).
/** * Register the core class aliases in the container. * * @return void */ public function registerCoreContainerAliases() { $aliases = [ "app" => ["IlluminateFoundationApplication", "IlluminateContractsContainerContainer", "IlluminateContractsFoundationApplication"], "auth" => "IlluminateAuthAuthManager", "auth.driver" => ["IlluminateAuthGuard", "IlluminateContractsAuthGuard"], "auth.password.tokens" => "IlluminateAuthPasswordsTokenRepositoryInterface", "blade.compiler" => "IlluminateViewCompilersBladeCompiler", "cache" => ["IlluminateCacheCacheManager", "IlluminateContractsCacheFactory"], "cache.store" => ["IlluminateCacheRepository", "IlluminateContractsCacheRepository"], "config" => ["IlluminateConfigRepository", "IlluminateContractsConfigRepository"], "cookie" => ["IlluminateCookieCookieJar", "IlluminateContractsCookieFactory", "IlluminateContractsCookieQueueingFactory"], "encrypter" => ["IlluminateEncryptionEncrypter", "IlluminateContractsEncryptionEncrypter"], "db" => "IlluminateDatabaseDatabaseManager", "db.connection" => ["IlluminateDatabaseConnection", "IlluminateDatabaseConnectionInterface"], "events" => ["IlluminateEventsDispatcher", "IlluminateContractsEventsDispatcher"], "files" => "IlluminateFilesystemFilesystem", "filesystem" => ["IlluminateFilesystemFilesystemManager", "IlluminateContractsFilesystemFactory"], "filesystem.disk" => "IlluminateContractsFilesystemFilesystem", "filesystem.cloud" => "IlluminateContractsFilesystemCloud", "hash" => "IlluminateContractsHashingHasher", "translator" => ["IlluminateTranslationTranslator", "SymfonyComponentTranslationTranslatorInterface"], "log" => ["IlluminateLogWriter", "IlluminateContractsLoggingLog", "PsrLogLoggerInterface"], "mailer" => ["IlluminateMailMailer", "IlluminateContractsMailMailer", "IlluminateContractsMailMailQueue"], "auth.password" => ["IlluminateAuthPasswordsPasswordBroker", "IlluminateContractsAuthPasswordBroker"], "queue" => ["IlluminateQueueQueueManager", "IlluminateContractsQueueFactory", "IlluminateContractsQueueMonitor"], "queue.connection" => "IlluminateContractsQueueQueue", "redirect" => "IlluminateRoutingRedirector", "redis" => ["IlluminateRedisDatabase", "IlluminateContractsRedisDatabase"], "request" => "IlluminateHttpRequest", "router" => ["IlluminateRoutingRouter", "IlluminateContractsRoutingRegistrar"], "session" => "IlluminateSessionSessionManager", "session.store" => ["IlluminateSessionStore", "SymfonyComponentHttpFoundationSessionSessionInterface"], "url" => ["IlluminateRoutingUrlGenerator", "IlluminateContractsRoutingUrlGenerator"], "validator" => ["IlluminateValidationFactory", "IlluminateContractsValidationFactory"], "view" => ["IlluminateViewFactory", "IlluminateContractsViewFactory"], ];
在我們自定義的接口實現(xiàn)時,我們可以在ServiceProvider中使用進行綁定:
$this->app->bind("AppContractsEventPusher", "AppServicesPusherEventPusher");Facades
Facades 為應用程序的服務容器中可用的類提供了一個「靜態(tài)」接口。Laravel 「facades」作為在服務容器內(nèi)基類的「靜態(tài)代理」。很難懂?
我們打開項目目錄下的config/app.php,然后找到
/* |-------------------------------------------------------------------------- | Class Aliases |-------------------------------------------------------------------------- | | This array of class aliases will be registered when this application | is started. However, feel free to register as many as you wish as | the aliases are "lazy" loaded so they don"t hinder performance. | */ "aliases" => [ "App" => IlluminateSupportFacadesApp::class, "Artisan" => IlluminateSupportFacadesArtisan::class, "Auth" => IlluminateSupportFacadesAuth::class, "Blade" => IlluminateSupportFacadesBlade::class, "Bus" => IlluminateSupportFacadesBus::class, "Cache" => IlluminateSupportFacadesCache::class, "Config" => IlluminateSupportFacadesConfig::class, "Cookie" => IlluminateSupportFacadesCookie::class, "Crypt" => IlluminateSupportFacadesCrypt::class, "DB" => IlluminateSupportFacadesDB::class, "Eloquent" => IlluminateDatabaseEloquentModel::class, "Event" => IlluminateSupportFacadesEvent::class, "File" => IlluminateSupportFacadesFile::class, "Gate" => IlluminateSupportFacadesGate::class, "Hash" => IlluminateSupportFacadesHash::class, "Input" => IlluminateSupportFacadesInput::class, "Lang" => IlluminateSupportFacadesLang::class, "Log" => IlluminateSupportFacadesLog::class, "Mail" => IlluminateSupportFacadesMail::class, "Password" => IlluminateSupportFacadesPassword::class, "Queue" => IlluminateSupportFacadesQueue::class, "Redirect" => IlluminateSupportFacadesRedirect::class, "Redis" => IlluminateSupportFacadesRedis::class, "Request" => IlluminateSupportFacadesRequest::class, "Response" => IlluminateSupportFacadesResponse::class, "Route" => IlluminateSupportFacadesRoute::class, "Schema" => IlluminateSupportFacadesSchema::class, "Session" => IlluminateSupportFacadesSession::class, "Storage" => IlluminateSupportFacadesStorage::class, "URL" => IlluminateSupportFacadesURL::class, "Validator" => IlluminateSupportFacadesValidator::class, "View" => IlluminateSupportFacadesView::class, ],
你是不是發(fā)現(xiàn)了什么?對,F(xiàn)acades其實就是在config/app.php中定義的一系列類的別名。只不過這些類都具有一個共同的特點,那就是繼承基底 IlluminateSupportFacadesFacade 類并實現(xiàn)一個方法:getFacadeAccessor返回名稱。
自定義Facade參考http://www.tutorialspoint.com/laravel/laravel_facades.htm
Step 1 ?創(chuàng)建一個名為 TestFacadesServiceProvider的ServiceProvider ,使用如下命令即可:
php artisan make:provider TestFacadesServiceProvider
Step 2 ? 創(chuàng)建一個底層代理類,命名為“TestFacades.php” at “App/Test”.
App/Test/TestFacades.php
Step 3 ? 創(chuàng)建一個 Facade 類 called “TestFacades.php” at “App/Test/Facades”.
App/Test/Facades/TestFacades.php
Step 4 ?創(chuàng)建一個ServiceProviders類,名為“TestFacadesServiceProviders.php” at “App/Test/Facades”.
App/Providers/TestFacadesServiceProviders.phpapp->bind("test", function(){ return new MyFoo(); //給這個Facade返回一個代理實例。所有對Facade的調(diào)用都會被轉發(fā)到該類對象下。 }); } }Step 5 ? 在config/app.php注冊ServiceProvider類
Step 6 ? 在config/app.php注冊自定義Facade的別名
使用測試:
Add the following lines in app/Http/routes.php.Route::get("/facadeex", function(){ return TestFacades::testingFacades(); });Step 9 ? Visit the following URL to test the Facade.
http://localhost:8000/facadeex去查看輸出
文章版權歸作者所有,未經(jīng)允許請勿轉載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉載請注明本文地址:http://specialneedsforspecialkids.com/yun/21517.html
摘要:的核心概念包括服務容器服務提供者門面契約。所有服務提供者都需要繼承類。可以為服務提供者的方法設置類型提示。方法將在所有其他服務提供者均已注冊之后調(diào)用。同樣會整理成思維導圖的形式以方便記憶與回顧。 showImg(https://segmentfault.com/img/remote/1460000010771201); Laravel 的核心概念包括:服務容器、服務提供者、門面(Fac...
摘要:前言年底了不太忙,最近一段時間也一直在研究,就想寫篇關于比較深一點的教程系列啥的,于是就找到站長給開了寫教程的渠道。優(yōu)點的就是為藝術家創(chuàng)造的框架,它也是工程化的趨勢。項目維護方便也是事實。如果有遇到問題可以直接在教程下面留言。 前言 年底了不太忙,最近一段時間也一直在研究laravel,就想寫篇關于laravel比較深一點的教程系列啥的,于是就找到站長給開了寫教程的渠道。由于第一次寫,...
摘要:最適合入門的初級教程一為什么選擇曾經(jīng)要跟白頭到老沒想到它升了個級就拋了錨把我等拋棄了痛定思痛重新審視了一遍框架是世界上最好的語言這個沒有疑問吧如果有那絕對是個異教徒這是要被拖出去燒死的信仰的問題神圣不可侵犯那最好的語言中最流行的框架是哪個呢 最適合入門的 Laravel 初級教程 (一) 為什么選擇 laravel 曾經(jīng)要跟 thinkphp 白頭到老;沒想到它升了個級就拋了錨;把我等...
摘要:過去一年時間寫了多篇文章來探討了我認為的框架最核心部分的設計思路代碼實現(xiàn)。為了大家閱讀方便,我把這些源碼學習的文章匯總到這里。數(shù)據(jù)庫算法和數(shù)據(jù)結構這些都是編程的內(nèi)功,只有內(nèi)功深厚了才能解決遇到的復雜問題。 過去一年時間寫了20多篇文章來探討了我認為的Larave框架最核心部分的設計思路、代碼實現(xiàn)。通過更新文章自己在軟件設計、文字表達方面都有所提高,在剛開始決定寫Laravel源碼分析地...
閱讀 3193·2021-11-23 10:09
閱讀 2057·2021-10-26 09:51
閱讀 975·2021-10-09 09:44
閱讀 3898·2021-10-08 10:04
閱讀 2742·2021-09-22 15:14
閱讀 3619·2021-09-22 15:02
閱讀 1035·2021-08-24 10:03
閱讀 1719·2019-12-27 12:14