mercredi 5 juillet 2017

Laravel container dependencies auto resolving

When is it necessary to explicitly register classes in the container?

Say I've got a class that depends on some standard Laravel's components:

class Dump {
     public function __construct(\Illuminate\Contracts\Config\Repository $config, 
    \Illuminate\Contracts\Filesystem\Filesystem $filesystem)
        {

        }
}

Normally I would do this thing in the service provider:

$this->app->singleton(Dump::class, function (Application $app) {
            return new Dump($app->make('config'), $app->make('files'));
        });

However, I see that it's redundant. Laravel resolves and injects these components itself if I do resolve(Dump::class). Just encountered with this in an integration test.

What if there is another class depending on Dump? Would Laravel resolve Dump and inject it into the depending custom class? Could you explain what it can/can't resolve and inject automatically and where - only the standard places like jobs/controllers or wherever? Seems like I'm missing something in the understanding of the container.



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2tfYb84
via IFTTT

Aucun commentaire:

Enregistrer un commentaire