我们可以在服务提供商中实现多态。
我在服务提供商
中有这个代码public function register()
{
$this->app->bind('App\Repositories\User\UserInterface', 'App\Repositories\User\UserRepository');
}
和我的UserController构造函数
public function __construct( UserInterface $user){
$this->user = $user;
}
到目前为止代码工作正常,但我还需要使用AdminRepository,我认为在寄存器中它是在UserRepository中耦合的。我该怎么办?
答案 0 :(得分:1)
您可以使用contextual binding
$this->app->when(AdminController::class)
->needs(UserInterface::class)
->give(AdminRepository::class);
$this->app->when(UserController::class)
->needs(UserInterface::class)
->give(UserRepository::class);