代码之家  ›  专栏  ›  技术社区  ›  ronline

自定义包存储库接口在Laravel 6中不可实例化

  •  0
  • ronline  · 技术社区  · 4 年前

    目标 [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;
                }
    

    我的config/app.php

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

    Composer.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 不修复任何问题,并抛出不可即时的错误

    0 回复  |  直到 4 年前
        1
  •  0
  •   ronline    4 年前

    取出

    $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);
            
        }
    
    
    }
    
    推荐文章