我发现了很多关于我的问题的问题并尝试了(我认为)所有的解决方案,但我无法让它发挥作用。我可能忽略了一些非常容易的事情。
我正在使用 Laravel 5。我尝试实现存储库模式。我有一个 Eloquent 模型 '\Notos\Kind'。
然后我有这个界面:
namespace Notos\Contracts;
interface Kind
{
public function findByName($name);
}
我的存储库如下所示:
namespace Notos\Repository;
use Illuminate\Database\Eloquent\Model;
use Notos\Contracts\Kind as KindContract;
class Kind implements KindContract
{
protected $kind;
public function __construct(Model $kind)
{
$this->kind = $kind;
}
public function findByName($name)
{
return $this->kind->firstOrCreate(array('name' => $name));
}
}
我的服务提供商:
namespace Notos\Providers;
use Illuminate\Support\ServiceProvider;
use Notos\Kind;
use Notos\Repository\Kind as KindRepository;
class RepoServiceProvider extends ServiceProvider
{
/**
* Register the service provider.
*
* @return void
*/
public function register()
{
$this->app->bind('Notos\Contracts\Kind', function ($app) {
return new KindRepository(new Kind);
});
}
}
还有我使用存储库的控制器:
namespace Notos\Http\Controllers;
use Notos\Repository\Kind as KindRepository;
class KindController extends Controller
{
protected $kind;
public function __construct(KindRepository $kind)
{
$this->kind = $kind;
}
public function find($name)
{
return $this->kind->findByName($name);
}
}
此提供程序在 config/app.php 中注册
当我尝试执行 KindController@find 时,出现此错误:
BindingResolutionException in Container.php line 785:
Target [Illuminate\Database\Eloquent\Model] is not instantiable.
我找不到我做错了什么。如果我将 __construct(Model $kind) 更改为 __construct(Kind $kind),它会完美运行。
有任何想法吗?谢谢