lundi 10 août 2020

Object loop in Laravel 7 and bind

I am beginner in Laravel. I use in my project Laravel 7 and Repository Pattern.

I have many ServiceProviders:

class VatTypesServiceProvider extends ServiceProvider
{
    public function register()
    {
        $this->app->bind(
            VatTypesRepositoryInterface::class,
            VatTypesRepository::class
        );
    }
}

class UserServiceProvider extends ServiceProvider
{
    public function register()
    {
        $this->app->bind(
            UserRepositoryInterface::class,
            UserRepository::class
        );
    }
}

I want to simplify my code and make one RepositoryServiceProvider file with all providers.

I make this code:

class RepositoryServiceProvider extends ServiceProvider
{

protected $providers = [
    User::class => UserRepository::class,
    VatTypes::class => VatTypesRepository::class,
];


    /**
     * Register services.
     *
     * @return void
     */
    public function register()
    {
        //$this->app->bind(
         //   VatTypesRepositoryInterface::class,
         //   VatTypesRepository::class
        //);


// here I want bind my $providers array
foreach($providers as $obj){
    $this->app->bind(
        $obj::class,
        $obj::class
    );
}



    }
}

Now I want make foreach and bind my all providers in one places.

How can I make it?

How to write correctly $ this-> app-> bind (
             $ Obj :: class,
             $ Obj :: class
         );

?

Please help me



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire