jeudi 22 décembre 2016

Is it possible to inject a repository as a dependency in a laravel service file?

What I am trying to do - 1) Controller will depend on service for logic and stuff. 2) Service with depend on repository for database access. 3) Controller will get service as a dependency. 4) Service will get a repository as a dependency.

My service class is -

class AnimeService implements IAnimeService{

    protected $animeRepository;

    public function _construct(IAnimeRepository $_animeRepository) {       
        $this->animeRepository = $_animeRepository;
    }

    public function add(Anime $anime) {      
        return $this->animeRepository->add($anime);
    }
}

problem is -

Call to a member function add() on null

which is expected - as the _construct is never being called.

By the way; from my AppServiceProvider

public function register()
    {
        $this->app->bind('\App\Services\Interfaces\IAnimeService', '\App\Services\Implementations\AnimeService');
        $this->app->bind('\App\Repositories\Interfaces\IAnimeRepository', '\App\Repositories\Implementations\AnimeRepository');
    }

My questions are - 1) What can I do in this situation to make my service work?

2) What is the best practice?

3) What other ways are there to achieve what I am trying to do?

Thank you.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire