在构建[App \ Http \ Controllers \ Member \ NewsController]时,如何解决Target [App \ Repositories \ NewsRepository]无法实例化。?

时间:2017-08-30 09:32:45

标签: laravel repository laravel-5.4

我的控制器是这样的:

...
use App\Repositories\NewsRepository;
class NewsController extends Controller
{
    protected $repository;
    public function __construct(NewsRepository $repository)
    {
        $this->repository = $repository;
    }
    public function store(NewsCreateRequest $request)
    {
        ...
        $news = $this->repository->create($input);
        ...
    }
}

我的界面是这样的:

namespace App\Repositories;
use Prettus\Repository\Contracts\RepositoryInterface;
interface NewsRepository extends RepositoryInterface
{
    //
}

我的班级是这样的:

...
use App\Repositories\NewsRepository;
class NewsRepositoryEloquent extends BaseRepository implements NewsRepository
{
    public function model()
    {
        return News::class;
    }
    public function boot()
    {
        $this->pushCriteria(app(RequestCriteria::class));
    }
}

我的存储库服务提供商是这样的:

...
class RepositoryServiceProvider extends ServiceProvider
{
    ...
    public function register()
    {
        ...
        $this->app->bind(\App\Repositories\NewsRepository::class, \App\Repositories\NewsRepositoryEloquent::class);
    }
}

如果执行控制器上的方法存储,则存在错误:

  

(1/1)BindingResolutionException目标   构建时,[App \ Repositories \ NewsRepository]不可实例化   [应用\ HTTP \控制器\会员\ NewsRepository]。

如何解决错误?

1 个答案:

答案 0 :(得分:0)

您必须将构造函数添加到您的存储库中,并像这样为设置模型调用父构造函数:

public function __construct(Permission $model)
{
    parent::__construct($model);
}