Laravel Creating a Service Provider Argument 1 Must Implement a Service Provider

Hi, I am creating a custom cache service class that will abstract the caching layer from my repository. However, I ran into some problems as I get this error:

Argument 1 passed to Task::__construct() must implement interface MyApp\Cache\CacheInterface, none given, called in /var/www/app/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php on line 792 and defined

My class is as follows:

<?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);
  }

}

And in my model, I have the following:

<?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;
}

And I have a cache service provider that looks like this:

<?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');

    }


}

Any ideas how I can properly configure this service provider for use in any mode / controller / repo, etc.

+4
source share
4 answers

You tried to do it like this:

<?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);
        });

    }
}

I just updated it in the comments on a business trip

+1
source

TaskRepository, , CacheService. , TaskRepository.

CacheService . CacheService, . , . $tag, , , .

- ​​ , CacheService.

.

, $tags.

$tags - , , CacheService.

$ - , , bind() .

$ - , , CacheService bind().. p >

$tags - , , . , TaskRepository , .

Laravel : http://laravel.com/docs/5.0/container

+1

, Eloquent. IoC , AbstractModel Eloquent Model, IoC . ( - https://github.com/laravel/framework/issues/3862#issuecomment-37364543)

:

(1) CacheService. Eloquent - ORM, , .

(2) CacheService Facade Laravel, -, - MyCache:: ($ key). Singleton, . (L4: http://laravel.com/docs/4.2/facades, L5: http://laravel.com/docs/5.1/facades)

(3) # 1 # 2, : http://dfg.gd/blog/decoupling-your-code-in-laravel-using-repositiories-and-services

+1

, CacheInterface SprintTask, CacheService, CacheInterface.

You need to implement this interface in CashServiceto pass it to SpringTask:

class CacheService implements CacheInterface
-1
source

Source: https://habr.com/ru/post/1599597/


All Articles