Laravel5中contracts詳解
我們先來看看官方文檔中對(duì)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è)接口,對(duì)應(yīng)一個(gè)框架核心服務(wù)。
那它的意義何在?官網(wǎng)給出的解釋也很簡(jiǎn)單:使用接口是為了 松耦合 和 簡(jiǎn)單 。
先不講大道理,先來點(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)手冊(cè)里有。我們就拿 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對(duì)象,也是容器對(duì)象,通過 $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ù),對(duì)應(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è)步驟:
向容器中注冊(cè)實(shí)現(xiàn)contract接口的對(duì)象。
構(gòu)造函數(shù)參數(shù)類型指定為contract接口類,框架會(huì)自動(dòng)找到符合條件的對(duì)象。
那么再來說說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è)問題的?請(qǐng)看代碼:
<?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。
簡(jiǎn)單性
如果所有服務(wù)都使用接口定義,就可以很簡(jiǎn)單的決定一個(gè)服務(wù)需要的功能,更加容易維護(hù)和擴(kuò)展,并且contract接口還能看作一個(gè)簡(jiǎn)潔的文檔便于閱讀。
- 詳解Laravel5.6 Passport實(shí)現(xiàn)Api接口認(rèn)證
- 在Laravel5.6中使用Swoole的協(xié)程數(shù)據(jù)庫查詢
- 淺析Laravel5中隊(duì)列的配置及使用
- Laravel5框架自定義錯(cuò)誤頁面配置操作示例
- Laravel5中Cookie的使用詳解
- Laravel5權(quán)限管理方法詳解
- Laravel5中防止XSS跨站攻擊的方法
- laravel5.4+vue+element簡(jiǎn)單搭建的示例代碼
- CKeditor4 字體顏色功能配置方法教程
- 手把手教你 CKEDITOR 4 擴(kuò)展插件制作
- CKEditor 4.4.1 添加代碼高亮顯示插件功能教程【使用官方推薦Code Snippet插件】
- Laravel5.6框架使用CKEditor5相關(guān)配置詳解
相關(guān)文章
php檢測(cè)網(wǎng)頁是否被百度收錄的函數(shù)代碼
下面給出一段php函數(shù),功能是檢測(cè)一個(gè)網(wǎng)頁是否被百度收錄,直接給出代碼2013-10-10初識(shí)通用數(shù)據(jù)庫操作類——前端easyui-datagrid,form(php)
這篇文章主要介紹了初識(shí)通用數(shù)據(jù)庫操作類——前端easyui-datagrid,form(php),實(shí)現(xiàn)代碼比較簡(jiǎn)單,有需要的小伙伴歡迎來參考2015-07-07PHP+shell腳本操作Memcached和Apache Status的實(shí)例分享
這篇文章主要介紹了PHP環(huán)境下使用shell腳本操作Memcached和Apache Status的方法,分別還可以控制Memcached進(jìn)程的啟動(dòng)以及記錄Apache Status數(shù)據(jù)到數(shù)據(jù)庫,需要的朋友可以參考下2016-03-03py文件轉(zhuǎn)exe時(shí)包含paramiko模塊出錯(cuò)解決方法
這篇文章主要介紹了py文件轉(zhuǎn)exe時(shí)包含paramiko模塊出錯(cuò)解決方法的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-08-08