Laravel 5 存储库注入

Laravel 5 repository injection

我是 Laravel 5 的新手,我想做的是一个带有依赖注入的简单存储库。但是我遇到了这个错误:

Argument 1 passed to App\Http\Controllers\Api\UserController::__construct() must implement interface App\Repositories\UserInterface, instance of App\Repositories\UserRepository given

这是我的代码:

用户控制器:

namespace App\Http\Controllers\Api;

    use App\Http\Controllers\Controller;
    use Illuminate\Http\Request;
    use Response;
    use App;
    use Auth;
    use Crypt;
    use Lang;
    use Image;
    use Storage;
    use Config;
    use Validator;
    use App\User;

    use App\Repositories\UserInterface;

    class UserController extends Controller
    {

        protected $config;
        protected $users;

        public function __construct(UserInterface $users)
        {
            $this->middleware('api');
            $this->middleware('auth', ['except' => 'getInfo']);

            $this->users = $users;

            $this->config = Config::get('images.avatar');
        }

用户界面:

namespace App\Repositories;

    use App\Repositories\BaseInterface;

    interface UserInterface extends BaseInterface
    {
    };

基础接口:

namespace App\Repositories;

    interface BaseInterface
    {
        public function all();
        public function paginate($count);
        public function find($id);
    }

BaseRepository

namespace App\Repositories;

    use App\Repositories\BaseInterface;

    class BaseRepository implements BaseInterface
    {

        protected $model;

        public function __call($name, $args)
        {
            //  $this->getNewInstance()->{$name($args)};
            return call_user_func_array([
            $this->getNewInstance(),
            $method], $args);
        }

        public function all($relations = [])
        {
            $instance = $this->getNewInstance();
            return $instance->with($relations)->all();
        }

        public function find($id, $relations = [])
        {
            $instance = $this->getNewInstance();
            return $instance->with($relations)->find($id);
        }

        public function findOrFail($id, $relations = [])
        {
            $instance = $this->getNewInstance();
            return $instance->with($relations)->findOrFail($id);
        }

        public function paginate($count)
        {

        }

        protected function getNewInstance()
        {
            return new $this->model;
        }

    }

用户资料库

namespace App\Repositories;

    use App\Repositories\BaseRepository;

    Class UserRepository extends BaseRepository
    {
        protected $model = 'App\User';
    }

RepositoryServiceProvider

namespace App\Providers;

    use Illuminate\Support\ServiceProvider;
    use App;

    class RepositoryServiceProvider extends ServiceProvider
    {

        /**
         * Register any error handlers.
         *
         * @return void
         */
        public function boot()
        {

        }

        /**
         * Register the service provider.
         *
         * @return void
         */
        public function register()
        {
            //
            App::bind('App\Repositories\UserInterface', 'App\Repositories\UserRepository');
        }

    }

当然RepositoryServiceProvider是在我的config/app服务提供商下添加的。php

请帮忙,我几乎可以肯定我已经尝试了我在 Google 中找到的所有内容。

您的 UserRepository 必须实施 UserInterface:

namespace App\Repositories;

use App\Repositories\BaseRepository;

class UserRepository extends BaseRepository implements UserInterface
//                                          ^^^^^^^^^^^^^^^^^^^^^^^^
{
    protected $model = 'App\User';
}