Laravel不希望在Service Provider中自动注入依赖项


Laravel does not want to auto-inject dependencies in Service Provider

我有一个服务提供商:

<?php namespace App'Providers;
use Carbon'Carbon;
use Illuminate'Support'Collection;
use Illuminate'Support'ServiceProvider;
use Illuminate'Contracts'Cache'Store;
use File;
use App'Models'Translation;
class ModuleTranslationsServiceProvider extends ServiceProvider {
    protected $cache;
    public function __construct (Store $cache) {
        $this->cache = $cache;
    }
    /**
     * Load the config from the database.
     *
     * @return void
     */
    public function register()
    {
        $translations = $this->cache->remember('translations', function() {
            //Load and return translations.
        });
        $this->app->singleton('translations', function($app) use ($translations) {
            return $translations;
        });
    }
}

然而,当应用程序运行时(工匠或浏览器),我得到以下错误:

ErrorException]                                                                                                                                                                                                                                                                        
  Argument 1 passed to App'Providers'ModuleTranslationsServiceProvider::__construct() must be an instance of Illuminate'Contracts'Cache'Store, instance of Illuminate'Foundation'Application given, called in /home/user/projects/AppPlatform/vendor/laravel/framework/  
  src/Illuminate/Foundation/ProviderRepository.php on line 150 and defined                                                                                                                                                                                                                

通常,Laravel通过构造函数自动注入合同。这是对服务提供商的限制吗?还是我做错了?

编辑:

我更改了代码以反映Iamzozo:的建议

public function boot(Store $cache) {
    $this->cache = $cache;
}

但这只回来了:

[Symfony'Component'Debug'Exception'FatalErrorException]  
Call to a member function remember() on null    

所以我尝试从应用程序实例化:

$cache = $this->app['Illuminate'Cache'CacheManager'];

但这只回来了:

[ReflectionException]       
Class cache does not exist

尝试实例化合同:

$cache = $this->app['Illuminate'Contracts'Cache'Store'];         

给我一个:

[Illuminate'Container'BindingResolutionException]               
Target [Illuminate'Contracts'Cache'Store] is not instantiable.  

我所知道的最后一件事:使用门面:

use Cache;
//...
$translations = Cache::remember( [...]

但这反过来又回来了:

[Symfony'Component'Debug'Exception'FatalErrorException]                
Call to undefined method Illuminate'Support'Facades'Cache::remember()  

所以我检查了我的提供商阵列:

'providers' => [
    /*
     * ...
     */
    'Illuminate'Cache'CacheServiceProvider',
    /*
     * ...
     */
    'App'Providers'ModuleTranslationsServiceProvider',
],

所以缓存是在我的服务提供商之前加载的。

在这一点上我没有新的想法。

php版本是通过Apache在php-FPM上运行的5.6.4。Laravel版本为5.0。

也许我不清楚我的答案,所以我更新了整个解决方案,这个解决方案适用于Laravel 5.1:

<?php namespace App'Providers;
use Carbon'Carbon;
use Illuminate'Support'Collection;
use Illuminate'Support'ServiceProvider;
use Illuminate'Cache'CacheManager as Cache;
use File;
use App'Models'Translation;
class ModuleTranslationsServiceProvider extends ServiceProvider {
    /**
     * Load the config from the database.
     *
     * @return void
     */
    public function register()
    {
        // Update for redis:
        $this->app->register('Illuminate'Redis'RedisServiceProvider::class);
        $cache = new Cache($this->app);
        $translations = $cache->remember('translations', function() {
            //Load and return translations.
        });
        $this->app->singleton('translations', function($app) use ($translations) {
            return $translations;
        });
    }
}

我没有意识到,你尝试使用的Cache'Store是一个接口。