Laravel5中contracts詳解_php執行個體

來源:互聯網
上載者:User
我們先來看看官方文檔中對contracts的定義:

Laravel's Contracts are a set of interfaces that define the core services provided by the framework.
意思是說Laravel的Contracts是一個由 架構提供 的定義了 核心服務介面 的集合。

也就是說,每一個Contract都是一個介面,對應一個架構核心服務。

那它的意義何在?官網給出的解釋也很簡單:使用介面是為了 松耦合 和 簡單 。

先不講大道理,先來點乾貨,看看怎麼使用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

…… 太多了,懶得繼續貼了,官網手冊裡有。我們就拿 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 方法我們綁定了一個實現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 建構函式:

複製代碼 代碼如下:
public function __construct(Guard $auth, Registrar $registrar)
{
$this->auth = $auth;
$this->registrar = $registrar;

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

它有兩個參數,對應的類命名空間在指令碼開頭可以看到:

複製代碼 代碼如下:
use Illuminate\Contracts\Auth\Guard;
use Illuminate\Contracts\Auth\Registrar;

這兩個都是contract,但我們這裡就拿 Registrar 說,我們注意到這裡面只是通過參數類型指明了$registrar的介面類型,而實際調用的時候實際上是 App\Services\Registrar 這個類,這就是依賴注入的特性了,Laravel會自動在容器中搜尋實現了介面Illuminate\Contracts\Auth\Registrar的類或對象,有的話就取出來作為實際參數傳到建構函式裡。

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

向容器中註冊實現contract介面的對象。
建構函式參數類型指定為contract介面類,架構會自動找到合格對象。
那麼再來說說contract的好處。

松耦合

官網給了一個例子解釋什麼是緊耦合以及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))
{
//
}
}
}

可以看到建構函式中注入了一個詳細的緩衝實現 \SomePackage\Cache\Memcached ,如果換Redis作為快取服務器或者更改了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;
}
}

注意,緩衝實現我們使用了一個介面,也就是contract,Illuminate\Contracts\Cache\Repository,因為它只是介面,不需要關心背後是memcache還是redis。

簡單性

如果所有服務都使用介面定義,就可以很簡單的決定一個服務需要的功能,更加容易維護和擴充,並且contract介面還能看作一個簡潔的文檔便於閱讀。

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.