我创建了一个简单的自定义 Symfony 包(在 5.0 版中)。这是在我跑到 之后composer require
,vendor/ntrx/ntrx-user-bundle
但我无法正常加载它。那里的文件夹结构是这样的:
Controller/
Service/
composer.json
NtrxUserBundle.php
Readme.md
composer.json
包含以下内容:
{
"name": "ntrx/ntrx-user-bundle",
"description": "",
"license": "proprietary",
"type": "symfony-bundle",
"require": {
"php": "^7.2.5",
[...]
},
"autoload": {
"psr-4": { "Ntrx\\UserBundle\\": "" }
}
}
并且NtrxUserBundle.php
包含以下代码:
<?php
namespace Ntrx\UserBundle;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\HttpKernel\Bundle\Bundle;
class NtrxUserBundle extends Bundle
{
/**
* {@inheritdoc}
*/
public function build(ContainerBuilder $container): void {
parent::build($container);
}
}
例如,当我运行时,php bin/console
我收到以下错误:
Symfony\Component\ErrorHandler\Error\ClassNotFoundError^ {#31
#message: """
Attempted to load class "NtrxUserBundle" from namespace "Ntrx\UserBundle".\n
Did you forget a "use" statement for another namespace?
"""
#code: 0
#file: "./src/Kernel.php"
#line: 23
trace: {
./src/Kernel.php:23 {
App\Kernel->registerBundles(): iterable^
› if ($envs[$this->environment] ?? $envs['all'] ?? false) {
› yield new $class();
› }
}
./vendor/symfony/http-kernel/Kernel.php:369 { …}
./vendor/symfony/http-kernel/Kernel.php:123 { …}
./vendor/symfony/framework-bundle/Console/Application.php:169 { …}
./vendor/symfony/framework-bundle/Console/Application.php:75 { …}
./vendor/symfony/console/Application.php:140 { …}
./bin/console:42 { …}
}
}
我试图更改捆绑包的名称或更改自动加载器,但似乎该类根本不存在。我还尝试破坏其他第三方捆绑包(在类名中打错字),在那里我得到了相应的错误,但没有上面的错误(The file was found but the class was not in it, the class name or namespace probably has a typo.
)。也composer dump-autoload
改变不了任何东西。
我在网上发现的唯一类似错误是Symfony 启用自定义捆绑 ClassNotFoundException,我认为我的代码中的所有内容都存在错误。有什么建议么?