jeudi 28 mars 2019

Laravel: Different implementations on an instance with Servcie Container

In Laravel, how do I resolve 2 different singleton implementations of an instance using Laravel's Service Container (https://laravel.com/docs/5.7/container).

For example, the 2 implementations I have for a Foo class is:

$this->app->singleton(Foo::class, function ($app) {
    return new Foo(config('services.foo.apiKey1'));
});

and

$this->app->singleton(Foo::class, function ($app) {
    return new Foo(config('services.foo.apiKey2'));
});

I then have to also resolve it somehow:

$fooV1 = app(Foo::class); // ?
$fooV2 = app(Foo::class); // ?

What is the correct way of writing and resolving 2 different singleton implementations of an instance?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire