简体   繁体   中英

Laravel Service provider not working

I've bind my interface called CustomerRepository to EloquentCustomerRepository . This is my CustomerServiceProvider :

public function register()
    {
        $this->app->bind(CustomerRepository::class,EloquentCustomerRepository::class);
        $this->app->bind(PackageRepository::class,EloquentPackageRepository::class);
    }

When I try to instantiate it in my controller like this:

<?php

namespace App\Http\Controllers\api\v1;

use Lsupport\repositories\api\v1\customer\CustomerRepository;
use App\Http\Controllers\Controller;
use Lsupport\customer\Customer;
use App\Http\Requests;

class CustomerController extends Controller
{
    protected $CustomerRepository;

    public function __construct(CustomerRepository $CustomerRepository)
    {
        $this->CustomerRepository = $CustomerRepository;
    }

It throws the following error:

Target [Lsupport\\repositories\\api\\v1\\Customer\\CustomerRepository] is not instantiable while building [App\\Http\\Controllers\\api\\v1\\CustomerController].

I also registered it in app.config :

App\Providers\CustomerServiceProvider::class,

What am I doing wrong?

CustomerServiceProvider

<?php

namespace App\Providers;

use Lsupport\repositories\api\v1\customer\EloquentCustomerRepository;
use Lsupport\repositories\api\v1\customer\EloquentPackageRepository;
use Lsupport\repositories\api\v1\customer\CustomerRepository;
use Lsupport\repositories\api\v1\customer\PackageRepository;
use Illuminate\Support\ServiceProvider;

class CustomerServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap the application services.
     *
     * @return void
     */
    public function boot()
    {
        //
    }

    /**
     * Register the application services.
     *
     * @return void
     */
    public function register()
    {
        $this->app->bind(CustomerRepository::class,EloquentCustomerRepository::class);
        $this->app->bind(PackageRepository::class,EloquentPackageRepository::class);
    }
}

CustomerRepository

    <?php

    namespace Lsupport\repositories\api\v1\Customer;

    interface CustomerRepository
    {
        public function create($request);
    }

**EloquentCustomerRepository**

<?php

namespace Lsupport\repositories\api\v1\customer;

use Lsupport\repositories\api\v1\customer\CusteromRepositoryTrait;
use Lsupport\repositories\api\v1\remain\RightTrait;
use Lsupport\repositories\api\v1\remain\JsonTrait;
use Lsupport\customer\Customer;

class EloquentCustomerRepository implements CustomerRepository
{
    use JsonTrait;
    use RightTrait;
    use CustomerRepositoryTrait;

    code.....

Ok, the first thing I notice is that you probably want the same namespaces on the interface and on the class. So, the namespace of EloquentCustomerRepository should be

namespace Lsupport\repositories\api\v1\Customer;

and not

namespace Lsupport\repositories\api\v1\customer;

(with lower customer).

Now, on your CustomerServiceProvider , you should use:

public function register()
{
    $this->app->bind('Lsupport\repositories\api\v1\Customer\CustomerRepository', 'Lsupport\repositories\api\v1\Customer\EloquentCustomerRepository');
}

Make sure you run composer dumpautoload -o on the command line.

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