欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Laravel5中contracts详解

程序员文章站 2023-01-15 11:15:29
我们先来看看官方文档中对contracts的定义: laravel's contracts are a set of interfaces that define the...

我们先来看看官方文档中对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接口还能看作一个简洁的文档便于阅读。