我创建了一个新的 symfony 5.3 项目。我正在使用六边形架构开发这个应用程序。
我添加了一个我为另一个 symfony 项目开发的 Bundle。此捆绑包在原始项目中工作。但在新项目中,我收到以下错误:
!! Fatal error: Cannot declare class UserBundle\Infrastructure\DependencyInjection\MycompanyUserExtension, because the name is already in use in /var/www/html/src/Mycompany/UserBundle/Infrastructure/DependencyInjection/MycompanyUserExtension.php on line 11 !! Symfony\Component\ErrorHandler\Error\FatalError {#419 !! -error: array:4 [ !! "type" => 64 !! "message" => "Cannot declare class UserBundle\Infrastructure\DependencyInjection\MycompanyUserExtension, because the name is already in use" !! "file" => "/var/www/html/src/Mycompany/UserBundle/Infrastructure/DependencyInjection/MycompanyUserExtension.php" !! "line" => 11 !! ] !! #message: "Compile Error: Cannot declare class UserBundle\Infrastructure\DependencyInjection\MycompanyUserExtension, because the name is already in use" !! #code: 0 !! #file: "./src/Mycompany/UserBundle/Infrastructure/DependencyInjection/MycompanyUserExtension.php" !! #line: 11 !! }
文件夹结构是:
src
|- Mycompany
|- UserBundle
|- MycompanyUserBundle.php
|- Domain
|- Infrastructure
|- DependencyInjection
| |- MycompanyUserExtension.php
|- Resources
|- config
|- routes.yaml
|- services.yaml
MycompanyUserBundle 的代码是:
# src/Mycompany/UserBundle/MycompanyUserBundle.php
namespace UserBundle;
use Symfony\Component\HttpKernel\Bundle\Bundle;
use UserBundle\Infrastructure\DependencyInjection\MycompanyUserExtension;
class MycompanyUserBundle extends Bundle
{
public function getContainerExtension()
{
if (null === $this->extension) {
$this->extension = new MycompanyUserExtension();
}
return $this->extension;
}
}
MycompanyUserExtension 的代码是:
# src/Mycompany/UserBundle/Infrastructure/DependencyInjection/MycompanyUserExtension.php
namespace UserBundle\Infrastructure\DependencyInjection;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Extension\Extension;
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
class MycompanyUserExtension extends Extension
{
public function load(array $configs, ContainerBuilder $container)
{
$loader = new YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
$loader->load('services.yaml');
}
public function getAlias()
{
return 'user_bundle';
}
}
命名空间 UserBundle 在 composer.json 中声明
"autoload": {
"psr-4": {
"UserBundle\\": "src/Mycompany/UserBundle",
"App\\": "src/"
}
},
并且捆绑也在 config/bundles.php 中声明为UserBundle\MycompanyUserBundle::class => ['all' => true],
该捆绑包也在映射下的 config/packages/doctrine.yaml 中引用
UserBundle:
is_bundle: false
type: annotation
dir: '%kernel.project_dir%/src/Mycompany/UserBundle/Domain/Entity'
prefix: 'UserBundle\Domain\Entity'
alias: UserBundle
MycompanyUserExtension
仅在错误说它正在使用的同一文件中声明,我没有找到此类的任何重复声明。但是项目没有编译,因为有错误。这是一个全新的项目,还没有任何代码或捆绑包。