Laravel IOC未调用构造函数

问题描述 投票:0回答:1

我在服务提供者中具有约束力

$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()
    {
    }
 }

在“注入了IInovoice服务”中的一个类中。

我正在获取IInovoice的具体实现。但从未调用过InvoiceService的构造函数

laravel laravel-5 ioc-container
1个回答
0
投票

尝试以下操作

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())
        ->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
© www.soinside.com 2019 - 2024. All rights reserved.