A
A
Alexey Nikolaev2018-09-25 12:24:26
Laravel
Alexey Nikolaev, 2018-09-25 12:24:26

Why can't Laravel inject the dependency?

Good day.
Created my daddy Contracts, which contains the DataSource interface. I expect an instance of DataSource in the controller method, but Laravel says that this class does not exist.

ReflectionException - Class App\Contracts\DataSource does not exist

That is, DI works, tries to resolve the name and connect the file, but does not find it. Although he is!
At the same time, in the service provider, I tied the interface to a specific implementation for the sake of testing.
public function register()
{
    $this->app->bind('App\Contracts\DataSource', function ($app) {
        return new App\DataSources\Source;
    });
}

Does not work. Why? How to solve the problem?
Thanks in advance.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
E
Evgeniy Samoilenko, 2018-09-25
@samoilenkoevgeniy

composer dump-autoload

A
Alexander Shapoval, 2018-09-25
@sanek_os9

Before App try \ put

$this->app->bind('\App\Contracts\DataSource', function ($app) {
        return new \App\DataSources\Source;
    });

or
use App\Contracts\DataSource;
use App\DataSources\Source;

and
$this->app->bind(DataSource::class, function ($app) {
        return new Source::class;
    });

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question