Skip to content

Instantly share code, notes, and snippets.

@sursir
Created April 11, 2018 10:47
Show Gist options
  • Save sursir/1ae3861436ed2cd08777d0e1b7a12163 to your computer and use it in GitHub Desktop.
Save sursir/1ae3861436ed2cd08777d0e1b7a12163 to your computer and use it in GitHub Desktop.
laravel contracts 契约

#自定义契约服务

// 比如缓存

namespace App\Extensions;

use Illuminate\Contracts\Cache\Store;

class MongoStore implements Store
{
    public function get($key) {}
    public function many(array $keys);

    // ... 实现各种接口
}
    // 其中'mongo'为驱动名称 如:`'driver' => 'mongo',`

    // 注册契约扩展(驱动) ServiceProvider
    public function boot()
    {
        Cache::extend('mongo', function ($app) {
            return Cache::repository(new App\Extensions\MongoStore);
        });
    }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment