国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 開(kāi)發(fā) > PHP > 正文

Laravel5中contracts詳解

2024-05-04 23:31:44
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友
在Laravel5中出現(xiàn)了一個(gè)新的東西,叫做contracts,那么它到底是什么?有什么用?怎么用?我們就來(lái)探討下吧。
 

我們先來(lái)看看官方文檔中對(duì)contracts的定義:

Laravel's Contracts are a set of interfaces that define the core services provided by the framework.
意思是說(shuō)Laravel的Contracts是一個(gè)由 框架提供 的定義了 核心服務(wù)接口 的集合。

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

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

先不講大道理,先來(lái)點(diǎn)干貨,看看怎么使用contract

先瀏覽下contracts接口列表:

 

復(fù)制代碼代碼如下:

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來(lái)演示一下吧。
首先,打開(kāi) app/Providers/AppServiceProvider.php,注意register方法:

 

復(fù)制代碼代碼如下:

public function register()
{
    $this->app->bind(
        'Illuminate/Contracts/Auth/Registrar',
        'App/Services/Registrar'
    );
}

 

$this->app 就是Application對(duì)象,也是容器對(duì)象,通過(guò) $this->app->bind 方法我們綁定了一個(gè)實(shí)現(xiàn)Illuminate/Contracts/Auth/Registrar接口的類(lèi)App/Services/Registrar。

注意,Illuminate/Contracts/Auth/Registrar就是一個(gè)contract。App/Services/Registrar 這個(gè)類(lèi)文件在 app/Services/Registrar.php。

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

 

復(fù)制代碼代碼如下:

public function __construct(Guard $auth, Registrar $registrar)
{
    $this->auth = $auth;
    $this->registrar = $registrar;

 

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

 

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

 

復(fù)制代碼代碼如下:

use Illuminate/Contracts/Auth/Guard;
use Illuminate/Contracts/Auth/Registrar;

 

這兩個(gè)都是contract,但我們這里就拿 Registrar 說(shuō),我們注意到這里面只是通過(guò)參數(shù)類(lèi)型指明了$registrar的接口類(lèi)型,而實(shí)際調(diào)用的時(shí)候?qū)嶋H上是 App/Services/Registrar 這個(gè)類(lèi),這就是依賴(lài)注入的特性了,Laravel會(huì)自動(dòng)在容器中搜索實(shí)現(xiàn)了接口Illuminate/Contracts/Auth/Registrar的類(lèi)或?qū)ο螅械脑捑腿〕鰜?lái)作為實(shí)際參數(shù)傳到構(gòu)造函數(shù)里。

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

向容器中注冊(cè)實(shí)現(xiàn)contract接口的對(duì)象。
構(gòu)造函數(shù)參數(shù)類(lèi)型指定為contract接口類(lèi),框架會(huì)自動(dòng)找到符合條件的對(duì)象。
那么再來(lái)說(shuō)說(shuō)contract的好處。

松耦合

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

先來(lái)看看緊耦合的代碼:

 

復(fù)制代碼代碼如下:

<?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è)問(wèn)題的?請(qǐng)看代碼:

 

復(fù)制代碼代碼如下:

<?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)樗皇墙涌冢恍枰P(guān)心背后是memcache還是redis。

簡(jiǎn)單性

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


發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 宜川县| 济阳县| 靖边县| 榆中县| 浪卡子县| 奎屯市| 天峨县| 沧源| 苍山县| 车险| 张家港市| 小金县| 泗水县| 昌都县| 道真| 油尖旺区| 隆化县| 高雄市| 汝城县| 青岛市| 日喀则市| 满洲里市| 临沂市| 河源市| 阳城县| 监利县| 宁强县| 精河县| 吉木萨尔县| 无极县| 友谊县| 兴安县| 武功县| 芷江| 柘城县| 安丘市| 皋兰县| 清河县| 闽侯县| 忻城县| 嘉禾县|