Laravel IO C не вызывает конструктор - PullRequest
0 голосов
/ 13 февраля 2020

У меня есть привязка в поставщике услуг

$this->app->singleton(
    'App\Models\Subscription\Interfaces\IInvoiceService',
    'App\Models\Subscription\Impl\InvoiceService'
);



class InvoiceService implements  IInvoiceService
{

    protected $repo;


    public function _construct(){

        $this->app = App::getFacadeRoot();

        $this->repo = $this->app['repo'];


    }
    public function Create()
    {
    }
 }

В одном из классов в Injected IInovoice Service.

Я получаю конкретную реализацию IInovoice. но конструктор InvoiceService никогда не вызывается

1 Ответ

0 голосов
/ 13 февраля 2020

Попробуйте следующее

class InvoiceService implements  IInvoiceService
{

    protected $app;
    protected $repo;

    public function _construct($app, $repo) {

        $this->app = $app;
        $this->repo = $repo;
    }

    public function Create() {

    }

 }

App \ Providers \ AppServiceProvider

public function register() {

      $this->app->singleton(
          'App\Models\Subscription\Interfaces\IInvoiceService',
          'App\Models\Subscription\Impl\InvoiceService'
      );


      $this->app->when('App\Models\Subscription\Impl\InvoiceService')
        ->needs('$app')
        ->give($this->app->getFacadeRoot());

      $this->app->when('App\Models\Subscription\Impl\InvoiceService')
        ->needs('$repo')
        ->give($this->app['repo']);
}

public function boot(ResponseFactory $response) {

      //uncomment this line to debug InvoiceService Resolving event

      //$this->app->resolving(App\Models\Subscription\Impl\InvoiceService::class, function ($invoiceService, $app)    {
        //dump('resolves InvoiceService', $invoiceService);
      //});
}

Затем выполните

php artisan clear-compiled
php artisan config:clear
...