Laravel服务提供商不工作

时间:2016-04-02 10:42:41

标签: php laravel

我已将名为CustomerRepository的界面绑定到EloquentCustomerRepository。这是我的CustomerServiceProvider

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

当我尝试在我的控制器中实例化它时:

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

它会抛出以下错误:

  

构建[App \ Http \ Controllers \ api \ v1 \ CustomerController]时,目标[Lsupport \ repositories \ api \ v1 \ Customer \ CustomerRepository]无法实例化。

我也在app.config注册了它:

App\Providers\CustomerServiceProvider::class,

我做错了什么?

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.....

1 个答案:

答案 0 :(得分:1)

好的,我注意到的第一件事是您可能希望在接口和类上使用相同的命名空间。因此,EloquentCustomerRepository的名称空间应为

namespace Lsupport\repositories\api\v1\Customer;

而不是

namespace Lsupport\repositories\api\v1\customer;

(顾客较低)。

现在,在您的CustomerServiceProvider上,您应该使用:

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

确保在命令行上运行composer dumpautoload -o