Laravel5中contracts詳解

字號(hào):


    在Laravel5中出現(xiàn)了一個(gè)新的東西,叫做contracts,那么它到底是什么?有什么用?怎么用?我們就來探討下吧。
    我們先來看看官方文檔中對contracts的定義:
    Laravel's Contracts are a set of interfaces that define the core services provided by the framework.
    意思是說Laravel的Contracts是一個(gè)由 框架提供 的定義了 核心服務(wù)接口 的集合。
    也就是說,每一個(gè)Contract都是一個(gè)接口,對應(yīng)一個(gè)框架核心服務(wù)。
    那它的意義何在?官網(wǎng)給出的解釋也很簡單:使用接口是為了 松耦合 和 簡單 。
    先不講大道理,先來點(diǎn)干貨,看看怎么使用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 這個(gè)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 方法我們綁定了一個(gè)實(shí)現(xiàn)Illuminate\Contracts\Auth\Registrar接口的類App\Services\Registrar。
    注意,Illuminate\Contracts\Auth\Registrar就是一個(gè)contract。App\Services\Registrar 這個(gè)類文件在 app/Services/Registrar.php。
    接著我們看 App\Http\Controllers\Auth\AuthController 這個(gè)控制器類,看到它有 __construct 構(gòu)造函數(shù):
    代碼如下:
    public function __construct(Guard $auth, Registrar $registrar)
    {
    $this->auth = $auth;
    $this->registrar = $registrar;
    $this->middleware('guest', ['except' => 'getLogout']);
    }
    它有兩個(gè)參數(shù),對應(yīng)的類命名空間在腳本開頭可以看到:
    代碼如下:
    use Illuminate\Contracts\Auth\Guard;
    use Illuminate\Contracts\Auth\Registrar;
    這兩個(gè)都是contract,但我們這里就拿 Registrar 說,我們注意到這里面只是通過參數(shù)類型指明了$registrar的接口類型,而實(shí)際調(diào)用的時(shí)候?qū)嶋H上是 App\Services\Registrar 這個(gè)類,這就是依賴注入的特性了,Laravel會(huì)自動(dòng)在容器中搜索實(shí)現(xiàn)了接口Illuminate\Contracts\Auth\Registrar的類或?qū)ο?,有的話就取出來作為?shí)際參數(shù)傳到構(gòu)造函數(shù)里。
    整個(gè)使用流程其實(shí)就可以總結(jié)為兩個(gè)步驟:
    向容器中注冊實(shí)現(xiàn)contract接口的對象。
    構(gòu)造函數(shù)參數(shù)類型指定為contract接口類,框架會(huì)自動(dòng)找到符合條件的對象。
    那么再來說說contract的好處。
    松耦合
    官網(wǎng)給了一個(gè)例子解釋什么是緊耦合以及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ù)中注入了一個(gè)詳細(xì)的緩存實(shí)現(xiàn) \SomePackage\Cache\Memcached ,如果換Redis作為緩存服務(wù)器或者更改了api方法,就需要修改,而如果項(xiàng)目很大,你不知道還有多少地方需要修改。
    那么,Contract接口是如何解決這個(gè)問題的?請看代碼:
    代碼如下:
    <?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;
    }
    }
    注意,緩存實(shí)現(xiàn)我們使用了一個(gè)接口,也就是contract,Illuminate\Contracts\Cache\Repository,因?yàn)樗皇墙涌?,不需要關(guān)心背后是memcache還是redis。
    簡單性
    如果所有服務(wù)都使用接口定義,就可以很簡單的決定一個(gè)服務(wù)需要的功能,更加容易維護(hù)和擴(kuò)展,并且contract接口還能看作一個(gè)簡潔的文檔便于閱讀。