【问题标题】:Why's my interface not instantiable even though I have what appears to be the correct files/implementation?为什么即使我有正确的文件/实现,我的界面也不能实例化?
【发布时间】:2021-11-21 16:34:38
【问题描述】:

尽管(据我所知)一切都已正确设置,但我不确定为什么会收到显示 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 类声明为abstract,因为如果我不这样做,我会在类名下方看到一条红色波浪线,并显示一条警告:

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,
]

【问题讨论】:

    标签: php laravel debugging


    【解决方案1】:

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

    第一个 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 处理依赖注入。

    【讨论】:

    • 我明白你在说什么。我已经尝试过了,但我仍然遇到同样的错误,是否在另一个文件中还有其他内容需要与这些更改一起更新?
    • 您是否尝试清除缓存? composer dump-autoload, php artisan clear-compiled, php artisan cache:clear
    • 是的,我有。错误消失了,但现在出现了一个新错误:Symfony\Component\Routing\Exception\RouteNotFoundException: Route [login] not defined. in file /var/www/vendor/laravel/framework/src/Illuminate/Routing/UrlGenerator.php on line 444。它指向与此无关的登录路线,我正在尝试解决原因。
    • 想通了,调用这条路由时没有加Bearer *token*,现在可以正常使用了。谢谢!
    • 非常欢迎您,我们很乐意为您提供帮助。如果我的回答对您有所帮助,请考虑将其标记为已接受的答案。谢谢你,祝你的项目好运。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多