mercredi 28 octobre 2015

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

I have a 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;
        });
    }

}

However I get the following error when the app is run (artisan or browser):

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                                                                                                                                                                                                                

Normally, Laravel auto-injects contracts via the constructor. Is this a limitation of Service Providers? Or am I doing it wrong?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire