未找到laravel接口类

时间:2013-11-22 01:32:09

标签: php laravel laravel-4

我创建了一个界面,到目前为止测试了模型和界面中的函数。

我现在尝试将它应用到我的控制器中,最初出现了错误:Class golfmanager\service\creator\TicketCreatorInterface does not exist

我已经进行了许多作曲家更新和作曲家转储自动加载但到目前为止没有任何变化。

我在composer.json中添加了一个条目来自动加载该类,如下所示:

"autoload": {
    "classmap": [
        "app/commands",
        "app/controllers",
        "app/models",
        "app/database/migrations",
        "app/database/seeds",
        "app/tests/TestCase.php",
        "app/helpers",
        "app/golfmanager"
    ],
    "psr-0": {
        "golfmanager": "app/"
    }
},

[更新更正]

我创建错误的控制器类如下所示:

use golfmanager\service\creator\TicketCreatorInterface;

 //controller manages the ticket books

class BooksController extends BaseController {

/**
 * Book Repository
 *
 * @var Book
 */
protected $book;
protected $ticket;

public function __construct(Book $book, TicketCreatorInterface $ticket)
{
    $this->book = $book;
    $this->ticket = $ticket;
}
}

我的界面是:

namespace golfmanager\service\creator;
//ticket creator interface

interface TicketCreatorInterface {

public function createTicket($input, $book);

 }

把头发拉出来 -

进行单元测试会引发错误,当我尝试在浏览器中查看页面时,我只是得到一个永不加载的空白页

我的服务提供商设置如下:

namespace golfmanager\service;

use Illuminate\Support\ServiceProvider;

class GolfmanagerServiceProvider extends ServiceProvider {

public function register()
{
    $this->app->bind(
        'app\golfmanager\service\creator\TicketCreatorInterface', 
        'app\golfmanager\service\creator\TicketCreator'
        ); 
}

}

我错过了一些看不见的东西 - 我哪里出错?

[我已将上述方法更新为最新尝试] 更新: 在config / app.php中,我在服务提供商下面有以下内容:

//custom service providers
    'golfmanager\service\GolfmanagerServiceProvider'

和我的测试:

use Mockery as m;
use Way\Tests\Factory;

class BooksTest extends TestCase {

public function __construct()
{
    $this->mock = m::mock('Eloquent', 'Book');
    $this->collection = m::mock('Illuminate\Database\Eloquent\Collection')->shouldDeferMissing();
}

public function setUp()
{
    parent::setUp();

    $this->attributes = Factory::book(['id' => 1]);
    $this->app->instance('Book', $this->mock);
}

public function tearDown()
{
    m::close();
}

public function testIndex()
{
    $this->mock->shouldReceive('all')->once()->andReturn($this->collection);
    $this->call('GET', 'books');

    $this->assertViewHas('books');
}

当前错误:

我运行测试时的错误是:

Cannot redeclare class TicketCreatorInterface in C:\wamp\www\golfmanager\golfmanager\app\golfmanager\service\creator\TicketCreatorInterface.php on line 5

当我浏览浏览器时,我得到:

Class golfmanager\service\creator\TicketCreatorInterface does not exist

非常困惑 - 我不知道应该解决

由于

2 个答案:

答案 0 :(得分:1)

您需要在psr-0文件中的autoload内添加composer.json哈希。

像这样:

"autoload": {
    "classmap": [
        "app/commands",
        "app/controllers",
        "app/models",
        "app/database/migrations",
        "app/database/seeds",
        "app/tests/TestCase.php",
        "app/helpers",
        "app/golfmanager"
    ],
    "psr-0": {
        "golfmanager": "app/"
    }
},

再次composer dump-autoload


修改

我注意到您在app/golfmanagerclassmap中加载了psr-0。从classmap中删除它,只留下psr-0条目,如下所示:

"autoload": {
    "classmap": [
        "app/commands",
        "app/controllers",
        "app/models",
        "app/database/migrations",
        "app/database/seeds",
        "app/tests/TestCase.php",
        "app/helpers"
    ],
    "psr-0": {
        "golfmanager": "app/"
    }
},

现在,像往常一样composer dump-autoload,你应该摆脱重复的错误。

答案 1 :(得分:0)

除了Manuel的答案和出色的帮助外,最后一个错误的元素就是命名空间。

接口存在于golfmanager \ service \ creator中,但我试图在app \ golfmanager \ service \ creator中引用它们

所以绑定现在是:

class GolfmanagerServiceProvider extends ServiceProvider {

public function register()
{
    $this->app->bind(
        'golfmanager\service\creator\TicketCreatorInterface', 
        'golfmanager\service\creator\TicketCreator'
        ); 
}

}