2017-05-06 123 views
0

我创建了一个名为ProviderLaravel工厂模式问题

class Provider 
{ 

    private $provider; 

    public function __construct(ProviderInterface $provider) 
    { 
     $this->provider = $provider; 
    } 
} 

类在工厂类,我有这样的:

class ProviderFactory 
{ 
    public static function adapt(string $provider) 
    { 
    switch ($provider) { 
      case 'ProviderOne': 
       return app(Provider::class, [app(ProviderOne::class)]); 
      case 'ProviderTwo': 
       return app(Provider::class, [app(ProviderTwo::class)]); 
     } 
    } 
} 

当调用ProviderFactory::adapt($request->provider)我得到一个错误:

Target [App\Services\ProviderInterface] is not instantiable while building [App\Services\Provider]. 

如果我理解正确,我需要bind()逻辑,因为它需要知道什么是双nd到ProviderInterface。我需要在服务提供商中添加bind()逻辑吗?

回答

0

下方添加到您的服务提供商的寄存器功能我假设你的接口有命名空间,如下

$this->app->bind('App\Services\ProviderInterface'); 
0

不宜$provider是公开的?

class Provider 
{ 

    public $provider; 

    public function __construct(ProviderInterface $provider) 
    { 
    $this->provider = $provider; 
    } 
}