Support the ongoing development of Laravel.io →
IOC Architecture
Last updated 2 years ago.
0

Your register method shouldn't be creating the instances.

In your case, your register needn't be more than:

public function register()
{
    $this->app->bind('UserRepositoryInterface', 'UserEloquentRepository');
}

You can change the class to something like ServiceProvider and register multiple bindings. For example:

public function register()
{
    $this->app->bind('UserRepositoryInterface', 'UserEloquentRepository');
    $this->app->bind('VehicleRepositoryInterface', 'VehicleEloquentRepository');
}
Last updated 2 years ago.
0

Ya I know, I accidentally copied that from a service provider where I was using the register method to register additional service providers.

I understand how to bind interfaces, but when you bind these you can only auto inject into Controllers (and some other special cases within Laravel). Did you take a look at the paste? I am trying to figure out how to auto inject additional dependencies without passing them into the constructor (either concrete or via bind)

Any help would be appreciated - I've put the past code here:


<?php

interface BaseRepositoryInterface {}

abstract class BaseEloquentRepository implements BaseRepositoryInterface {

    protected $model;

    protected $repoDep;

    public function __construct(Model $model, RepoDep $repoDep)
    {
        $this->model = $model;
    }

}

interface UserRepositoryInterface extends BaseRepositoryInterface {}


class UserEloquentRepository extends BaseEloquentRepository implements UserRepositoryInterface
{
    // I feel like there is a way to bind the RepoDep above so that 
    // I don't have to pass it to the constructor / service provider below

    public function __construct(User $user)
    {
        parent::__construct($user);
    }

}


// I'm auto injecting the model using service providers
// when resolving the Repos but again I don't want to pass in the 
// repoDep above into the service provider

class UserServiceProvider extends ServiceProvider {

    public function register()
    {
        $this->app->bind('UserRepositoryInterface',function (){
                return new UserEloquentRepository(new User());
            }
        );

        // want to avoid this
         return new UserEloquentRepository(new User(), $this->app->make('RepoDep'));
    }
}


// The only way I have found is to resolve any deps directly in the constructor
// of these base classes - but it just doesn't feel right

public function __construct(Model $model) 
{
     $this->model = $model;

     $this->app = App::getFacadeRoot();

     $this->dispatcher = $this->app->make('Illuminate\Contracts\Events\Dispatcher');
}

Last updated 2 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

ryantbrown ryantbrown Joined 20 Sep 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.