0

Target[MyPackage\Crm\App\Repositories\CommentRepositoryInterface]在构建时不可实例化[MyPackage\Crm\App\Http\CommentController]

MyPackage\Crm\App\Http\CommentControllers.php如果我将存储库作为对象注入,则有问题的控制器可以正常工作

public function __construct( \MyPackage\Crm\App\Repositories\CommentRepository\CommentRepository $commentRepository )
            {
                $this->noteRepo = $commentRepository; 
            }

但是如果我尝试注入 CommentRepositoryInterface 则会崩溃。

public function __construct( \MyPackage\Crm\App\Repositories\CommentRepository\CommentRepositoryInterface $commentRepository )
            {
                $this->noteRepo = $commentRepository;
            }

我的配置/app.php

return [
MyPackage\Crm\App\CommentServiceProvider::class
]

作曲家.json

    "MyPackage\\Crm\\":        "packages/mypackage/crm/src"

界面

namespace MyPackage\Crm\App\Repositories;
{

interface CommentRepositoryInterface
{
    public function create( int $userId );
}
}

存储库类

namespace MyPackage\Crm\App\Repositories;


class CommentRepository implements CommentRepositoryInterface
{
    public function create(int $userId)
    {
        // TODO: Implement create() method.
    }
.....
}

我的自定义包提供程序类

namespace MyPackage\Crm\App;

use Illuminate\Support\ServiceProvider;
use MyPackage\Crm\App\Repositories\CommentRepositoryInterface;
use MyPackage\Crm\App\Repositories\CommentRepository;

class CommentServiceProvider extends ServiceProvider
{

    /**
     * Bootstrap the application services.
     *
     * @return void
     */
    public function boot()
    {
        include __DIR__.'/routes.php';

    }

    public function register()
    {

        $this->app->make('MyPackage\Crm\App\Http\CommentController');
        $this->app->bind(CommentRepositoryInterface::class, CommentRepository::class);
        
    }


}

php artisan clear-compiled不修复任何东西并抛出不可实例化的错误

4

1 回答 1

0

取出

$this->app->make('MyPackage\Crm\App\Http\CommentController');

从提供者注册方法解决问题。

工作提供者代码:

namespace MyPackage\Crm\App;

use Illuminate\Support\ServiceProvider;
use MyPackage\Crm\App\Repositories\CommentRepositoryInterface;
use MyPackage\Crm\App\Repositories\CommentRepository;

class CommentServiceProvider extends ServiceProvider
{

    /**
     * Bootstrap the application services.
     *
     * @return void
     */
    public function boot()
    {
        include __DIR__.'/routes.php';

    }

    public function register()
    {
        $this->app->bind(CommentRepositoryInterface::class, CommentRepository::class);
        
    }


}
于 2021-01-10T02:39:10.683 回答