Christophe
Christophe

Reputation: 147

Laravel 4: Target Interface is not instantiable

I've been comparing my code to this question and many other guides online but with little success. Everything works fine until I try to inject the the interface in my controller. When I do inject it, I get the Target Interface is not instantiable error.

app\models\Interfaces\InterviewInterface.php

<?php namespace app\models\Interfaces;

interface InterviewInterface {
    public function fetch($id);
}

app\models\Interview.php

use app\models\Interfaces\InterviewInterface;

class Interview extends Eloquent implements InterviewInterface  {

    public function fetch($id)
    {
        return Interview::find($id);
    }

}

routes.php

App::bind('app\models\Interfaces\InterviewInterface');

composer.json

"psr-4": {
        "Blog\\Articles\\" : "app/lib/Blog/Articles",
        "app\\models\\Interfaces\\" : "app/models/Interfaces/InterviewInterface.php"
    }

AdminInterviewController.php

use app\models\Interfaces\InterviewInterface as InterviewInterface;

class AdminInterviewController extends BaseController  {

    public function __construct(InterviewInterface $interview)
    {
         $this->interview = $interview;
         $this->beforeFilter('auth');
    }

}

As soon as I add the

use app\models\Interfaces\InterviewInterface as InterviewInterface;

and

__construct(InterviewInterface $interview)
$this->interview = $interview;

lines, it gives me the error. I pull them out, no error.

I have run php composer.phar dump-autoload and php artisan dump-autoload commands multiple times and they succeed.

Any ideas? I really appreciate it.

Upvotes: 1

Views: 1072

Answers (1)

Laravelian
Laravelian

Reputation: 598

You need to bind the interface to a class in order to resolve it from the IOC:

In routes.php, assuming it is not namespaced:

App::bind('app\modesl\Interfaces\InterviewInterface', 'Interview');

Upvotes: 2

Related Questions