2016-01-24 93 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”。 –

相关问题