Laravel 5 如何在Laravel服务提供商中传递构造函数依赖关系
我正在服务提供商中注册我的社交身份验证服务,接口的实现需要其构造函数中的参数 如何通过服务提供商传递?这是我的代码,但语法不正确Laravel 5 如何在Laravel服务提供商中传递构造函数依赖关系,laravel-5,Laravel 5,我正在服务提供商中注册我的社交身份验证服务,接口的实现需要其构造函数中的参数 如何通过服务提供商传递?这是我的代码,但语法不正确 namespace App\Providers; use Illuminate\Support\ServiceProvider; use App; use App\User; use Illuminate\Contracts\Auth\Guard; use Laravel\Socialite\Contracts\Factory as Socialite; cla
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);
});
}
}
正确的方法是使用new关键字,而不是将依赖项指定给变量。并确保您的类在类的顶部导入(例如使用Guard)
public function register()
{
App::bind('App\Repositories\SocialAuthenticationInterface', function () {
return new App\Repositories\SocialAuthentication(new Socialite, new Guard, new User);
});
}
它返回一个致命错误,“无法实例化接口Laravel\Socialite\Contracts\Factory”。