【问题标题】:Laravel 5 repository injectionLaravel 5 存储库注入
【发布时间】:2015-03-09 09:18:54
【问题描述】:

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

参数 1 传递给 App\Http\Controllers\Api\UserController::__construct() 必须实现 接口 App\Repositories\UserInterface,实例 App\Repositories\UserRepository 给定

这是我的代码:

用户控制器:

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 中找到的所有内容。

【问题讨论】:

    标签: php dependency-injection laravel-5


    【解决方案1】:

    您的UserRepository 必须实现UserInterface

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-04-17
      • 2016-12-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-25
      • 2015-06-18
      相关资源
      最近更新 更多