首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Laravel 4:目标接口不可实例化

Laravel 4:目标接口不可实例化
EN

Stack Overflow用户
提问于 2015-01-09 17:29:06
回答 1查看 954关注 0票数 1

我一直在比较我的代码to this question和其他许多在线指南,但是没有什么成功。在我尝试将接口注入我的控制器之前,一切都很好。当我注入它时,我得到的目标接口不是可实例化的错误。

app\models\Interfaces\InterviewInterface.php

代码语言:javascript
复制
<?php namespace app\models\Interfaces;

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

app\models\Interview.php

代码语言:javascript
复制
use app\models\Interfaces\InterviewInterface;

class Interview extends Eloquent implements InterviewInterface  {

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

}

routes.php

代码语言:javascript
复制
App::bind('app\models\Interfaces\InterviewInterface');

composer.json

代码语言:javascript
复制
"psr-4": {
        "Blog\\Articles\\" : "app/lib/Blog/Articles",
        "app\\models\\Interfaces\\" : "app/models/Interfaces/InterviewInterface.php"
    }

AdminInterviewController.php

代码语言:javascript
复制
use app\models\Interfaces\InterviewInterface as InterviewInterface;

class AdminInterviewController extends BaseController  {

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

}

一旦我添加了

代码语言:javascript
复制
use app\models\Interfaces\InterviewInterface as InterviewInterface;

代码语言:javascript
复制
__construct(InterviewInterface $interview)
$this->interview = $interview;

线,它给了我错误。我把它们拉出来,没有错误。

我已经运行了php composer.phar转储-autoload和php手工转储-autoload命令多次,它们成功了。

有什么想法吗?我真的很感激。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-01-09 17:43:12

您需要将接口绑定到类,以便从IOC解析它:

在routes.php中,假设它不是名称空间:

代码语言:javascript
复制
App::bind('app\modesl\Interfaces\InterviewInterface', 'Interview');
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27865983

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档