2016-01-24 56 views
0

我正在服務提供者中註冊我的社交認證服務,我的接口的實現需要參數在其構造函數中。如何在Laravel服務提供者中傳遞構造函數依賴關係

如何通過服務提供者傳遞它?這是我的代碼,但它在語法上不正確。

namespace App\Providers; 

use Illuminate\Support\ServiceProvider; 

use App; 
use App\User; 
use Illuminate\Contracts\Auth\Guard; 
use Laravel\Socialite\Contracts\Factory as Socialite; 

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

    /** 
    * Register the application services. 
    * 
    * @return void 
    */ 
    public function register() 
    { 
     App::bind('App\Repositories\SocialAuthenticationInterface', function() { 
      return new App\Repositories\SocialAuthentication(Socialite $socialite, Guard $auth, User $user); 
     }); 
    } 
} 

回答

0

正確的方法是使用new關鍵字,而不是將您的依賴關係指定給變量。並確保您的課程在課程頂部導入(例如,使用Guard)。

public function register() 
{ 
    App::bind('App\Repositories\SocialAuthenticationInterface', function() { 
     return new App\Repositories\SocialAuthentication(new Socialite, new Guard, new User); 
    }); 
} 
+0

它返回一個致命錯誤,「無法實例化界面Laravel \ Socialite \ Contracts \ Factory」。 –

相關問題