Laravel中ServiceProvider使用場景示例詳解
ServiceProvider 的方式接入到 Laravel
有些朋友說,看了很多資料也不太明白 ServiceProvider
到底是干嘛用的,今天我試圖用大白話聊一聊 ServiceProvier
。
設(shè)想一個(gè)場景,你寫了一個(gè)CMS,那自然就包含了路由、配置、數(shù)據(jù)庫遷移、幫助函數(shù)或類等。如果你要用 ServiceProvider
的方式接入到 Laravel
,應(yīng)該怎么辦?
我們在上述用了 “接入到 Laravel
” 這樣的字眼,本質(zhì)上就是把這些信息告訴 Kernel
。如何告訴呢?使用 Laravel
提供的 ServiceProvider
,默認(rèn) ServiceProvider
要提供兩個(gè)方法 register
和 boot
。
register
就是把實(shí)例化對象的方式注冊到容器中。
boot
就是做一些把配置文件推到項(xiàng)目根目錄下的 config
目錄下面,加載配置到 Kernel
或加載路由等動(dòng)作。
順序是先 register
再 boot
。
源碼驗(yàn)證
這點(diǎn)可以在源碼中得到佐證:
干說也無趣,分析一個(gè)開源的 ServiceProvider
更直觀。
https://github.com/tymondesig...
開源組件的 ServiceProvider
看這個(gè)開源組件的 ServiceProvider
是怎么寫的:
https://github.com/tymondesig...
public function boot() { $path = realpath(__DIR__.'/../../config/config.php'); $this->publishes([$path => config_path('jwt.php')], 'config'); $this->mergeConfigFrom($path, 'jwt'); $this->aliasMiddleware(); $this->extendAuthGuard(); }
非常簡單,把配置文件推到 config
目錄下,加載配置文件,給中間件設(shè)置一個(gè)別名,擴(kuò)展一下 AuthGuard
。
看它的基類 https://github.com/tymondesig...
public function register() { $this->registerAliases(); $this->registerJWTProvider(); $this->registerAuthProvider(); $this->registerStorageProvider(); $this->registerJWTBlacklist(); $this->registerManager(); $this->registerTokenParser(); $this->registerJWT(); $this->registerJWTAuth(); $this->registerPayloadValidator(); $this->registerClaimFactory(); $this->registerPayloadFactory(); $this->registerJWTCommand(); $this->commands('tymon.jwt.secret'); } protected function registerNamshiProvider() { $this->app->singleton('tymon.jwt.provider.jwt.namshi', function ($app) { return new Namshi( new JWS(['typ' => 'JWT', 'alg' => $this->config('algo')]), $this->config('secret'), $this->config('algo'), $this->config('keys') ); }); } /** * Register the bindings for the Lcobucci JWT provider. * * @return void */ protected function registerLcobucciProvider() { $this->app->singleton('tymon.jwt.provider.jwt.lcobucci', function ($app) { return new Lcobucci( new JWTBuilder(), new JWTParser(), $this->config('secret'), $this->config('algo'), $this->config('keys') ); }); }
本質(zhì)上就是注冊一些實(shí)例化對象的方法到容器,用于后來的自動(dòng)裝配,解決注入的依賴問題。
所以 ServiceProvider
本質(zhì)上是個(gè)啥?它就是提供接入 Laravel
的方式,它本身并不實(shí)現(xiàn)具體功能,只是將你寫好的功能以 Laravel
能識(shí)別的方式接入進(jìn)去。
以上就是Laravel中ServiceProvider使用示例詳解的詳細(xì)內(nèi)容,更多關(guān)于Laravel ServiceProvider的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
php結(jié)合ajax實(shí)現(xiàn)贊、頂、踩功能實(shí)例
這篇文章主要介紹了php結(jié)合ajax實(shí)現(xiàn)贊、頂、踩功能實(shí)例,包含PHP代碼、JS代碼和數(shù)據(jù)庫端代碼,非常詳細(xì),入門非常好的例子,需要的朋友可以參考下2014-05-05PHP實(shí)現(xiàn)根據(jù)密碼長度顯示安全條
本文通過實(shí)例代碼給大家分享php根據(jù)密碼長度顯示安全條功能,代碼簡單易懂,非常不錯(cuò),具有參考借鑒價(jià)值,需要的的朋友參考下吧2017-07-07你可能不知道PHP get_meta_tags()函數(shù)
這篇文章主要介紹了你可能不知道PHP get_meta_tags()函數(shù),比較實(shí)用的一個(gè)函數(shù)了,小編也是第一次看到它,需要的朋友可以參考下2014-05-05ECshop 遷移到 PHP7版本時(shí)遇到的兼容性問題
最近有網(wǎng)友問我在php7上安裝ecshopv2.7.3時(shí),報(bào)錯(cuò),究竟了半天沒有找到原因,下面由腳本之家小編給大家分析此問題出現(xiàn)的原因2016-02-02excellent!——ASCII Art(由目標(biāo)圖象生成ascii)
這篇文章主要介紹了excellent!——ASCII Art2007-02-02php 使用curl模擬登錄人人(校內(nèi))網(wǎng)的簡單實(shí)例
下面小編就為大家?guī)硪黄猵hp 使用curl模擬登錄人人(校內(nèi))網(wǎng)的簡單實(shí)例。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-06-06