Caching 流明缓存\存储不可实例化
我对拉威尔和卢明还很陌生,所以我的问题可能有点简单,但我还没有找到任何有用的答案 流明版本为Caching 流明缓存\存储不可实例化,caching,laravel-5,lumen,Caching,Laravel 5,Lumen,我对拉威尔和卢明还很陌生,所以我的问题可能有点简单,但我还没有找到任何有用的答案 流明版本为5.1 所以我尝试创建一个由缓存支持的数据存储库。首先我想使用文件存储,然后我想使用更合适的文件存储 我尝试按如下方式注入缓存存储库: <?php namespace App\Repositories; use Illuminate\Cache\Repository; class DataRepository { private $cache;
5.1
所以我尝试创建一个由缓存支持的数据存储库。首先我想使用文件存储,然后我想使用更合适的文件存储
我尝试按如下方式注入缓存存储库:
<?php
namespace App\Repositories;
use Illuminate\Cache\Repository;
class DataRepository
{
private $cache;
public function __construct(Repository $cache)
{
$this->cache = $cache;
}
}
$this->app->bind(\Illuminate\Contracts\Cache\Store::class, \Illuminate\Cache\FileStore::class);
我猜存储库找不到匹配且可用的存储实现。当我尝试将存储绑定到\Illumante\Cache\FileStore时,如下所示:
<?php
namespace App\Repositories;
use Illuminate\Cache\Repository;
class DataRepository
{
private $cache;
public function __construct(Repository $cache)
{
$this->cache = $cache;
}
}
$this->app->bind(\Illuminate\Contracts\Cache\Store::class, \Illuminate\Cache\FileStore::class);
我发现了一种新的错误:
Unresolvable dependency resolving [Parameter #1 [ <required> $directory ]] in class Illuminate\Cache\FileStore
我试图添加cache.php配置。在bootstrap/app.php中,我添加了$app->configure('cache')
将其与以下配置一起使用:
<?php
return [
'default' => env('CACHE_DRIVER', 'file'),
'stores' => [
'file' => [
'driver' => 'file',
'path' => storage_path('framework/cache'),
],
],
];
答案
Lumen中的缓存实现注册为:
Illuminate\Contracts\Cache\Repository
不是
Illuminate\Cache\Repository
因此,您可以将代码更改为:
<?php
namespace App\Repositories;
use Illuminate\Contracts\Cache\Repository;
class DataRepository
{
private $cache;
public function __construct(Repository $cache)
{
$this->cache = $cache;
}
}
谢谢,就是这样:)
use Illuminate\Cache\Repository as CacheImplementation;
use Illuminate\Contracts\Cache\Repository as CacheContract;
$app->singleton(CacheImplementation::class, CacheContract::class);