Tuesday, April 4, 2017

Laravel repository non instantiable

UserRepository

<?php

namespace App\Repositories\UserRepository\UserRepository;

use App\Repositories\UserRepository\UserRepositoryInterface\UserRepositoryInterface;

use DB;
use App\Models\Course;
use App\Models\User;

use Illuminate\Support\Facades\Hash;

class UserRepository implements UserRepositoryInterface
{

    /**
     * Ger user by id
     *
     * @param $id
     * @return Models/User
     */
    public function getById(int $id)
    {
        return User::find($id);
    }
}

UserRepositoryInterface

<?php

namespace App\Repositories\UserRepository\UserRepositoryInterface;

use App\Models\User;
use App\Models\Course;

interface UserRepositoryInterface {

    /**
     * Ger user by id
     *
     * @param $id
     * @return Models/User
     */

    public function getById(int $id);
}

The folder structure is the following:

App/Repositories/UserRepository/UserRepository.php

App/Repositories/UserRepository/UserRepositoryInterface.php

AppServiceProvider Register method:

$this->app->bind('\App\Repositories\UserRepository\UserRepositoryInterface', function()
        {
            return new \App\Repositories\UserRepository\UserRepositoryInterface();
        });

And this is how I am injecting the repository into my controller:

<?php

namespace App\Http\Controllers;

use App\Http\Requests;
use Illuminate\Http\Request;

use App\Repositories\CourseRepository\CourseRepositoryInterface\CourseRepositoryInterface;
use App\Repositories\UserRepository\UserRepositoryInterface\UserRepositoryInterface;

class HomeController extends Controller
{
    /**
     * @var
     */
    protected $courseRepository;

    /**
     * @var
     */
    protected $userRepository;

    /**
     * @var
     */
    protected $user;

    /**
     * Create a new controller instance.
     *
     * @param UserRepositoryInterface $userRepository
     * @param CourseRepositoryInterface $courseRepository
     */
    public function __construct(UserRepositoryInterface $userRepository,
                                CourseRepositoryInterface $courseRepository)
    {
        $this->courseRepository = $courseRepository;
        $this->userRepository = $userRepository;
        $this->user = \Auth::user();
    }

The error that I am getting is:

Target [App\Repositories\UserRepository\UserRepositoryInterface\UserRepositoryInterface] is not instantiable while building [App\Http\Controllers\HomeController].

What am I doing wrong?



via kjanko

Advertisement