简体   繁体   中英

Custom Package Repository Interface is not instantiable in Laravel 6

Target [MyPackage\Crm\App\Repositories\CommentRepositoryInterface] is not instantiable while building [MyPackage\Crm\App\Http\CommentController] .

The Controller in question MyPackage\Crm\App\Http\CommentControllers.php works fine if I inject the Repository as Object

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

but crashes if I try to inject the CommentRepositoryInterface.

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

My config/app.php

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

Composer.json

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

Interface

namespace MyPackage\Crm\App\Repositories;
{

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

Repository class

namespace MyPackage\Crm\App\Repositories;


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

My custom package provider class

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 doesn't fix anything and throws the not instantiable error

Taking out the

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

from the provider register method fix the issue.

Working provider code:

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);
        
    }


}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM