Laravel 创建服务提供者参数 1 必须实现服务提供者

标签 laravel caching service laravel-4 ioc-container

您好,我正在创建一个自定义缓存服务类,它将从我的存储库中抽象出缓存层。但是,当我收到此错误时,我遇到了一些麻烦:

传递给 Task::__construct() 的参数 1 必须实现接口(interface) MyApp\Cache\CacheInterface,未给出,在/var/www/app/vendor/laravel/framework/src/Illuminate/Database/Eloquent 中调用/Model.php 第 792 行并定义

我的类(class)如下:

<?php namespace MyApp\Cache;

use Illuminate\Cache\CacheManager;

class CacheService {

 /**
  * @var Illuminate\Cache\CacheManager
  */
  protected $cache;

  /**
   * @var integer
   */
  protected $minutes;

  /**
   * Construct
   *
   * @param Illuminate\Cache\CacheManager $cache
   * @param string $tag
   * @param integer $minutes
   */
  public function __construct(CacheManager $cache, $minutes = 60)
  {
    $this->cache = $cache;
    $this->tag = $tag;
    $this->minutes = $minutes;
  }

  /**
   * Get
   *
   * @param string $key
   * @return mixed
   */
  public function get($key)
  {
    return $this->cache->tags($this->tag)->get($key);
  }

  /**
   * Put
   *
   * @param string $key
   * @param mixed $value
   * @param integer $minutes
   * @return mixed
   */
  public function put($key, $value, $minutes = null)
  {
    if( is_null($minutes) )
    {
      $minutes = $this->minutes;
    }

    return $this->cache->tags($this->tag)->put($key, $value, $minutes);
  }

  /**
   * Has
   *
   * @param string $key
   * @return bool
   */
  public function has($key)
  {
    return $this->cache->tags($this->tag)->has($key);
  }

}

在我的模型中,我有以下内容;

<?php
use Abstracts\Model as AbstractModel;
use Illuminate\Support\Collection;
use CMS\APIv2\Objects\Entity;
use MyApp/Cache\CacheInterface;

class SprintTask extends AbstractModel
{


    /**
     * @var CacheInterface
     */
    protected $cache;

    public function __construct(CacheInterface $cache)
    {
        $this->cache = $cache;
    }


public static function scopegetAssignedSprint($id) {
    $key = md5('id.'.$id.get_class());

    if($this->cache->has($key))
    {
        return $this->cache->get($key);
    }

    $user = static::where('uid', $id)->lists('sprint_id');

    $this->cache->put($key, $user);

    return $user;
}

我有一个缓存服务提供商,如下;

<?php
namespace MyApp\Cache;

use MyApp\Cache\CacheInterface;
use Illuminate\Support\ServiceProvider;

class CacheServiceProvider extends ServiceProvider
{

    /**
     * Indicates if loading of the provider is deferred.
     *
     * @var bool
     */
    protected $defer = false;

    /**
     * Register
     */
    public function register()
    {

        $this->app->bind
        ('MyApp\Cache\CacheInterface',
            'MyApp\Cache\CacheService');

    }


}

有什么想法可以正确设置此服务提供程序以在任何模式/ Controller /存储库等中使用?

最佳答案

您是否尝试过这样做:

<?php
namespace MyApp\Cache;

use MyApp\Cache\CacheInterface;
use Illuminate\Support\ServiceProvider;
use Illuminate\Cache\CacheManager;

class CacheServiceProvider extends ServiceProvider
{

    /**
     * Indicates if loading of the provider is deferred.
     *
     * @var bool
     */
    protected $defer = false;

    /**
     * Register
     */
    public function register()
    {

        $this->app->bind('MyApp\Cache\CacheInterface',function(){
            return new CacheService(CacheManager $cache);
        });

    }
}

我刚刚根据旅行评论更新了它

关于Laravel 创建服务提供者参数 1 必须实现服务提供者,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31621232/

相关文章:

java - 如何在 servlet 应用程序中实现服务层

php - 监听模型事件时无限循环

laravel - 在验证之前修改请求响应,Laravel

php - 如何删除 Laravel 中的重复行

ruby-on-rails - Rails 缓存(内容与页面/操作/等)

service - 使用 Powershell 中的 InstallUtil 安装服务(接受安装参数)

delphi - 有什么方法可以限制CodeCentral Web服务返回多少项目

php - 如何在共享主机 (CPanel) 上的 Laravel 中分派(dispatch)排队作业

ruby-on-rails - 在 Rails 4 上的开发中禁用 sprockets Assets 缓存

java - 缓存文件服务器