茄子在线看片免费人成视频,午夜福利精品a在线观看,国产高清自产拍在线观看,久久综合久久狠狠综合

    <s id="ddbnn"></s>
  • <sub id="ddbnn"><ol id="ddbnn"></ol></sub>

  • <legend id="ddbnn"></legend><s id="ddbnn"></s>

    Laravel5中contracts詳解
    來源:易賢網(wǎng) 閱讀:1024 次 日期:2015-03-04 10:36:40
    溫馨提示:易賢網(wǎng)小編為您整理了“Laravel5中contracts詳解”,方便廣大網(wǎng)友查閱!

    在Laravel5中出現(xiàn)了一個新的東西,叫做contracts,那么它到底是什么?有什么用?怎么用?我們就來探討下吧。

    我們先來看看官方文檔中對contracts的定義:

    Laravel's Contracts are a set of interfaces that define the core services provided by the framework.

    意思是說Laravel的Contracts是一個由 框架提供 的定義了 核心服務(wù)接口 的集合。

    也就是說,每一個Contract都是一個接口,對應(yīng)一個框架核心服務(wù)。

    那它的意義何在?官網(wǎng)給出的解釋也很簡單:使用接口是為了 松耦合 和 簡單 。

    先不講大道理,先來點干貨,看看怎么使用contract

    先瀏覽下contracts接口列表:

    代碼如下:

    Illuminate\Contracts\Auth\Guard

    Illuminate\Contracts\Auth\PasswordBroker

    Illuminate\Contracts\Bus\Dispatcher

    Illuminate\Contracts\Cache\Repository

    Illuminate\Contracts\Cache\Factory

    Illuminate\Contracts\Config\Repository

    Illuminate\Contracts\Container\Container

    Illuminate\Contracts\Cookie\Factory

    Illuminate\Contracts\Cookie\QueueingFactory

    Illuminate\Contracts\Encryption\Encrypter

    Illuminate\Contracts\Routing\Registrar

    …… 太多了,懶得繼續(xù)貼了,官網(wǎng)手冊里有。我們就拿 Illuminate\Contracts\Routing\Registrar 這個contract來演示一下吧。

    首先,打開 app/Providers/AppServiceProvider.php,注意register方法:

    代碼如下:

    public function register()

    {

    $this->app->bind(

    'Illuminate\Contracts\Auth\Registrar',

    'App\Services\Registrar'

    );

    }

    $this->app 就是Application對象,也是容器對象,通過 $this->app->bind 方法我們綁定了一個實現(xiàn)Illuminate\Contracts\Auth\Registrar接口的類App\Services\Registrar。

    注意,Illuminate\Contracts\Auth\Registrar就是一個contract。App\Services\Registrar 這個類文件在 app/Services/Registrar.php。

    接著我們看 App\Http\Controllers\Auth\AuthController 這個控制器類,看到它有 __construct 構(gòu)造函數(shù):

    代碼如下:

    public function __construct(Guard $auth, Registrar $registrar)

    {

    $this->auth = $auth;

    $this->registrar = $registrar;

    $this->middleware('guest', ['except' => 'getLogout']);

    }

    它有兩個參數(shù),對應(yīng)的類命名空間在腳本開頭可以看到:

    代碼如下:

    use Illuminate\Contracts\Auth\Guard;

    use Illuminate\Contracts\Auth\Registrar;

    這兩個都是contract,但我們這里就拿 Registrar 說,我們注意到這里面只是通過參數(shù)類型指明了$registrar的接口類型,而實際調(diào)用的時候?qū)嶋H上是 App\Services\Registrar 這個類,這就是依賴注入的特性了,Laravel會自動在容器中搜索實現(xiàn)了接口Illuminate\Contracts\Auth\Registrar的類或?qū)ο?,有的話就取出來作為實際參數(shù)傳到構(gòu)造函數(shù)里。

    整個使用流程其實就可以總結(jié)為兩個步驟:

    向容器中注冊實現(xiàn)contract接口的對象。

    構(gòu)造函數(shù)參數(shù)類型指定為contract接口類,框架會自動找到符合條件的對象。

    那么再來說說contract的好處。

    松耦合

    官網(wǎng)給了一個例子解釋什么是緊耦合以及Contract接口為何能夠松耦合。

    先來看看緊耦合的代碼:

    代碼如下:

    <?php namespace App\Orders;

    class Repository {

    /**

    * The cache.

    */

    protected $cache;

    /**

    * Create a new repository instance.

    *

    * @param \SomePackage\Cache\Memcached $cache

    * @return void

    */

    public function __construct(\SomePackage\Cache\Memcached $cache)

    {

    $this->cache = $cache;

    }

    /**

    * Retrieve an Order by ID.

    *

    * @param int $id

    * @return Order

    */

    public function find($id)

    {

    if ($this->cache->has($id))

    {

    //

    }

    }

    }

    可以看到構(gòu)造函數(shù)中注入了一個詳細(xì)的緩存實現(xiàn) \SomePackage\Cache\Memcached ,如果換Redis作為緩存服務(wù)器或者更改了api方法,就需要修改,而如果項目很大,你不知道還有多少地方需要修改。

    那么,Contract接口是如何解決這個問題的?請看代碼:

    代碼如下:

    <?php namespace App\Orders;

    use Illuminate\Contracts\Cache\Repository as Cache;

    class Repository {

    /**

    * Create a new repository instance.

    *

    * @param Cache $cache

    * @return void

    */

    public function __construct(Cache $cache)

    {

    $this->cache = $cache;

    }

    }

    注意,緩存實現(xiàn)我們使用了一個接口,也就是contract,Illuminate\Contracts\Cache\Repository,因為它只是接口,不需要關(guān)心背后是memcache還是redis。

    簡單性

    如果所有服務(wù)都使用接口定義,就可以很簡單的決定一個服務(wù)需要的功能,更加容易維護和擴展,并且contract接口還能看作一個簡潔的文檔便于閱讀。

    更多信息請查看IT技術(shù)專欄

    更多信息請查看網(wǎng)絡(luò)編程
    易賢網(wǎng)手機網(wǎng)站地址:Laravel5中contracts詳解
    由于各方面情況的不斷調(diào)整與變化,易賢網(wǎng)提供的所有考試信息和咨詢回復(fù)僅供參考,敬請考生以權(quán)威部門公布的正式信息和咨詢?yōu)闇?zhǔn)!

    2026上岸·考公考編培訓(xùn)報班

    • 報班類型
    • 姓名
    • 手機號
    • 驗證碼
    關(guān)于我們 | 聯(lián)系我們 | 人才招聘 | 網(wǎng)站聲明 | 網(wǎng)站幫助 | 非正式的簡要咨詢 | 簡要咨詢須知 | 新媒體/短視頻平臺 | 手機站點 | 投訴建議
    工業(yè)和信息化部備案號:滇ICP備2023014141號-1 云南省教育廳備案號:云教ICP備0901021 滇公網(wǎng)安備53010202001879號 人力資源服務(wù)許可證:(云)人服證字(2023)第0102001523號
    云南網(wǎng)警備案專用圖標(biāo)
    聯(lián)系電話:0871-65099533/13759567129 獲取招聘考試信息及咨詢關(guān)注公眾號:hfpxwx
    咨詢QQ:1093837350(9:00—18:00)版權(quán)所有:易賢網(wǎng)
    云南網(wǎng)警報警專用圖標(biāo)