2017-08-29 28 views
1

我綁定的接口,它的實現,當目標未實例化的,就像這樣:

<?php 

namespace App\Providers; 

use Illuminate\Support\ServiceProvider; 
use App\Services\Mail\Contracts\Webhook; 
use App\Services\Mail\Clients\MailgunWebhook; 

class MailServiceProvider extends ServiceProvider { 

    protected $defer = true; 

    /** 
    * Bootstrap the application services. 
    * 
    * @return void 
    */ 
    public function boot() { 
     // 
    } 

    /** 
    * Register the application services. 
    * 
    * @return void 
    */ 
    public function register() { 

     $this->app->bind(
      Webhook::class, 
      MailgunWebhook::class 
     ); 
    } 
} 

我跑所有這些:

php artisan config:clear 
php artisan clear-compiled 
php artisan optimize 
composer dumpautoload 

然而,當試圖使用綁定時,我仍然得到了「目標不可實例化錯誤」

當我註釋掉$defer屬性後,綁定開始工作。

爲什麼我不能使用$defer在這種情況下?

+1

你在哪裏實現'provide'方法? –

回答

2

正如documentation所言,這是我自己聯繫並未能完全讀它,$defer需要provides()方法。

在我而言,所有我需要在我的服務提供商添加類是這樣的:

public function provides() { 

    return array(Webhook::class); 
} 

感謝詹姆斯·芬威克爲他的評論。