2

不久前,我创建了一个树枝扩展。最近我添加了一个命名空间,以便我可以更轻松地与 composer 一起使用它。我已经这样做了几次,它适用于其他文件/类。

问题是,如果我在作曲家中更新或转储自动加载时使用 -o 选项(优化),它只会正确自动加载。

因此,例如,如果我运行composer dumpautoload -o我的课程,发现没有问题。如果我运行composer dumpautoload它将不起作用并给我以下错误:

Fatal error: Class 'Crecket\custom_twig_extension' not found in C:\Dropbox\Ampps\www\crecket.dev\index.php on line 24

我的扩展类:

namespace Crecket;

class custom_twig_extension extends \Twig_Extension{
    // The code
}

我的作曲家文件

"autoload": {
    "psr-4": {
        "Crecket\\": "src/"
    }
}

文件夹结构

src/custom_twig_extension.php

有谁知道是什么原因造成的?我似乎无法弄清楚是什么原因造成的。我猜这与我使用优化时创建类图的作曲家有关。

4

1 回答 1

0

I was having a similar issue, which turned out to be because the class name in my file was different to the filename providing it. I had myclass.php and class MyClass { }

According to the reddit discussion about someone encountering a similar issue (https://www.reddit.com/r/laravel/comments/2zx3in/l5_composer_dumpautoload_gives_class_not_found/):

In PSR-4, you have to name the file the name of the class.

When I renamed myclass.php to MyClass.php the issue disappeared - double check you've used the same name (and case!) for both!

于 2016-08-12T21:54:19.540 回答