Question

I am using UserRepositoryInterface in my controller but it doesn't find it. I am using laravel 4. My controller is like below:

 class Insur_DocController extends \BaseController {

       /*
        * Constructor.
        *

        public function __construct(UserRepositoryInterface $userInstance) {
            $cars = DB::table('cars')->orderBy('Description', 'asc')->distinct()->lists('Description', 'id');
            $this->cars = $cars;
        }

The error that it shows is:

ReflectionException Class UserRepositoryInterface does not exist

Was it helpful?

Solution

An interface is not a class and is not instantiable, so you have to bind that repository interface to your implementation of that interface, assuming that you have created both of them, in your routes.php (or just create a bindings.php), you can:

App::bind('UserRepositoryInterface', 'DbUserRepository');

And, of course, you have to use it in the top of the class which is using it:

use App\WhateverNamespaceYouHaveSetForYourRepositores\UserRepositoryInterface

If you didn't created an UserInterfaceRepository and a DbUserRepository, create them. If you don't want to create them you can just instantiate your user by using the User class:

public function __construct(User $userInstance) {
    ...
}

EDIT:

This is how I use my repositories:

I have a repository interface

<?php namespace App\Repositories\User;

interface RepositoryInterface {}

This is the database implementation of my repository:

<?php namespace App\Repositories\User;

use App\Repositories\BaseRepository;

use App\Repositories\User\RepositoryInterface as UserRepositoryInterface;

use App\Models\User\ModelInterface as UserModelInterface;

class DbRepository extends BaseRepository implements UserRepositoryInterface {

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

}

As you can see I have a BaseRepository abstract class, with some common methods implemented.

<?php namespace App\Repositories;

abstract class BaseRepository {

    protected $model;

    public function find($id)
    {
        ...
    }

    public function create($data)
    {
        ...
    }

    public function all($columns = null)
    {
        ...
    }

}

I also have a Models directory with a User in it, but my user model extends Cartalyst's Sentry and not Eloquent directly:

<?php namespace App\Models\User;

use Cartalyst\Sentry\Users\Eloquent\User as SentryModel;

use App\Models\BaseTraits as BaseModelTraits;

class Model extends SentryModel {

    protected $table = 'users';

}

And a binding:

App::bind('App\Repositories\User\RepositoryInterface', 'App\Repositories\User\DbRepository')

This is my directory structure

├── app
│   ├── App
│   │   ├── Models
│   │   │   ├── User
│   │   │   │   └── Model.php
│   │   ├── Repositories
│   │   │   ├── BaseRepository.php
│   │   │   ├── User
│   │   │   │   ├── RepositoryInterface.php
│   │   │   │   └── Repository.php

OTHER TIPS

What is the namespace for you UserRepositoryInterface?

For example if you have it in.

Acme\Repositories\UserRepositoryInterface

then in your controller you would do.

<?php 

use Acme\Repositories\UserRepositoryInterface;

class Insur_DocController extends \BaseController {

       /*
        * Constructor.
        *

        public function __construct(UserRepositoryInterface $userInstance) {
            $cars = DB::table('cars')->orderBy('Description', 'asc')->distinct()->lists('Description', 'id');
            $this->cars = $cars;
        }

Also have you created a bind for the interface?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top