未找到 Laravel 接口类

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20135340/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 08:40:55  来源:igfitidea点击:

laravel interface class not found

phplaravellaravel-4

提问by Ray

I have created an interface and so far tested the functions in the model and the interface.

我已经创建了一个界面,到目前为止测试了模型和界面中的功能。

I've now tried to apply it in my controller and initially came up with an error of: Class golfmanager\service\creator\TicketCreatorInterface does not exist

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

I have carried out a number of composer updates and composer dump-autoload but no change so far.

我已经进行了许多 Composer 更新和 composer dump-autoload 但到目前为止没有任何变化。

I added an entry to composer.json to autoload the class as follows:

我在 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/"
    }
},

[updated with corrections]

[更新更正]

My controller class which is creating the error looks like this:

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

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;
}
}

My interface is:

我的界面是:

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

interface TicketCreatorInterface {

public function createTicket($input, $book);

 }

Pulling my hair out over this -

把我的头发拉出来——

Doing a unit test throws up the error, when I try to view the page in the browser I simply get a blank page which never loads

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

I have a service provider set up as follows:

我有一个服务提供商,设置如下:

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'
        ); 
}

}

I've missed something obvious just can't see it - where have I gone wrong?

我错过了一些明显的东西,只是看不到它 - 我哪里出错了?

[I've updated the methods above to the latest attempts] Update: In config/app.php I have the following under service providers:

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

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

and my test:

和我的测试:

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');
}

Current Error:

当前错误:

At the moment the error when I run the test is:

目前我运行测试时的错误是:

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

When I run through the browser I get:

当我通过浏览器运行时,我得到:

Class golfmanager\service\creator\TicketCreatorInterface does not exist

very confused - I don't know enought to resolve

很困惑 - 我不知道解决

Thanks

谢谢

回答by Manuel Pedrera

You need to include the psr-0hash inside autoloadin your composer.jsonfile.

您需要在文件中包含psr-0哈希值。autoloadcomposer.json

Like this:

像这样:

"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/"
    }
},

And then composer dump-autoloadagain.

然后composer dump-autoload再一次。



Edit

编辑

I've noticed that you are loading your app/golfmanagerboth in classmapand psr-0. Remove it from classmap, leaving only the psr-0entry, like this:

我注意到你正在加载你app/golfmanagerclassmappsr-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/"
    }
},

Now, composer dump-autoloadas usual, and you should get rid of the duplicated error.

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

回答by Ray

In addition to Manuel's answers and excellent help the final element that was wrong was in the namespace.

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

The interfaces existed in golfmanager\service\creator but I was trying to reference them in app\golfmanager\service\creator

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

so the binding is now:

所以绑定现在是:

class GolfmanagerServiceProvider extends ServiceProvider {

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

}