jeudi 10 novembre 2016

How to pass arguments from resolve() to callback specified in give()?

Constructor of class A requires an instance of class B, whose constructor in turn takes an optional string argument.

I want to be able to specify the value of that string argument to class B constructor when resolving class A. I want to do something like this in my service provider:

$this->app
    ->when(A::class)
    ->needs(B::class)
    ->give(function($argument_for_b /* <-- LOOK HERE */){

        return new B($argument_for_b);
     })

and then obtain instance of A like this (assuming A has been bound to AInterface in the container):

$aInstance = resolve(AInterface::class /* HOW TO PASS ARGUMENT FOR B? */);



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire