为什么我的接口不能实例化,即使我有看起来正确的 files/implementation?

Why's my interface not instantiable even though I have what appears to be the correct files/implementation?

尽管(据我所知)所有设置都正确,但我不确定为什么会收到此错误 Illuminate\Contracts\Container\BindingResolutionException: Target [App\Dal\Interfaces\IUploadsRepository] is not instantiable while building [App\Http\Controllers\FileUploadController]. in file /var/www/vendor/laravel/framework/src/Illuminate/Container/Container.php on line 1093。拼写和其他一切都是正确的,但我仍然不确定问题出在哪里。

我已经尽了一切努力来完成这项工作,但我不确定我做错了什么。

我错过了什么?

注意: 我需要将 UploadsRepository.php class 声明为 abstract 因为如果我不这样做,那么我会得到一个红色class 名称下方的波浪线带有警告:

Class must be declared abstract or implement methods 'resetScope', 'hidden', 'syncWithoutDetaching', 'update', 'paginate', 'delete', 'findWhereBetween', 'whereHas', 'withCount', 'find', 'getFieldsSearchable', 'create', 'findWhereNotIn', 'setPresenter', 'skipPresenter', 'all', '__callStatic', 'findWhere', 'visible', 'simplePaginate', 'firstOrNew', 'orderBy', 'sync', 'scopeQuery', 'findWhereIn', 'findByField', 'with', 'lists', 'firstOrCreate', 'updateOrCreate', '__call', 'pluck' 

我不确定这是否是问题的根源,但我只想提供尽可能多的信息。

这是FileUploadController.php

<?php

namespace App\Http\Controllers;

use App\Dal\Interfaces\IUploadsRepository;
Use App\Dal\Repositories\UploadsRepository;

class FileUploadController extends Controller
{

    protected $__uploadsRepository;


    public function __construct(IUploadsRepository $uploadsRepository)
    {
        $this->__uploadsRepository = $uploadsRepository;
    }

    public function getUploads(): string
    {
        return $this->__uploadsRepository->getUploads();
    }
}

这里是IUploadsRepository.php(界面):

<?php

namespace App\Dal\Interfaces;
use Prettus\Repository\Contracts\RepositoryInterface;

interface IUploadsRepository extends RepositoryInterface
{
    public function getUploads();
}

这是UploadsRepository.php

<?php

namespace App\Dal\Repositories;

use App\Dal\Interfaces\IUploadsRepository;

abstract class UploadsRepository implements IUploadsRepository
{
    /**
     * @return string
     */
    public function getUploads(): string
    {
        return "test";
    }
}

这里是RepositoryServiceProvider.php

<?php

namespace App\Providers;

use App\Dal\Interfaces\IUploadsRepository;
use App\Dal\Repositories\UploadsRepository;
use Illuminate\Support\ServiceProvider;

class RepositoryServiceProvider extends ServiceProvider
{
    public function register() {
        $this->app->bind(IUploadsRepository::class,UploadsRepository::class);
    }
}

这里是config/app.php

'providers' => [
    Prettus\Repository\Providers\RepositoryServiceProvider::class,
    \App\Providers\RepositoryServiceProvider::class,
]

您正在扩展的 RepositoryInterface 定义了您在错误中看到的所有那些方法:“'resetScope'、'hidden'、'syncWithoutDetaching'、'update'...”。您的具体实现 UploadsRepository 仅实现 getUploads()。为了履行 RepositoryInterface 定义的契约,您的具体实现还需要实现其他方法。我的建议是不要实现该接口,而是让您的 UploadsRepository 从 Prettus\Repository\Eloquent\BaseRepository class 扩展,它为这些方法提供默认实现。你可以这样声明。

首先IUploadsRepository不需要扩展RepositoryInterface,所以声明为:

<?php

namespace App\Dal\Interfaces;

interface IUploadsRepository
{
    public function getUploads();
}

然后具体实现是这样的:

<?php

namespace App\Dal\Repositories;

use App\Dal\Interfaces\IUploadsRepository;
use Prettus\Repository\Eloquent\BaseRepository

class UploadsRepository extends BaseRepository implements IUploadsRepository
{
    /**
     * @return string
     */
    public function getUploads(): string
    {
        return "test";
    }
}

您现在拥有缺失方法的默认实现,并且可以使用 IUploadsRepository 接口通过 RepositoryServiceProvider 处理依赖项注入。