我一直在比较我的代码to this question和其他许多在线指南,但是没有什么成功。在我尝试将接口注入我的控制器之前,一切都很好。当我注入它时,我得到的目标接口不是可实例化的错误。
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');
}
}一旦我添加了
use app\models\Interfaces\InterviewInterface as InterviewInterface;和
__construct(InterviewInterface $interview)
$this->interview = $interview;线,它给了我错误。我把它们拉出来,没有错误。
我已经运行了php composer.phar转储-autoload和php手工转储-autoload命令多次,它们成功了。
有什么想法吗?我真的很感激。
发布于 2015-01-09 17:43:12
您需要将接口绑定到类,以便从IOC解析它:
在routes.php中,假设它不是名称空间:
App::bind('app\modesl\Interfaces\InterviewInterface', 'Interview');https://stackoverflow.com/questions/27865983
复制相似问题