我正在尝试在symfony4项目上使用SonataAdminBundle和SonataUserBundle。当我尝试扩展SonataUserBundle时,出现一个错误,指出我的新类已在使用中:
Fatal error: Cannot declare class Application\Sonata\UserBundle\ApplicationSonat
aUserBundle, because the name is already in use in C:\wamp64\www\staff-test\src\
Application\Sonata\UserBundle\ApplicationSonataUserBundle.php on line 24
我在bundles.php文件上启用了捆绑软件 在composer.json文件上,我在psr4属性上添加了新的捆绑软件。 我将新的捆绑软件映射到了学说上 我的ApplicationSonataUserBundle类仅在我的捆绑包中使用。我扩展了它:
php bin/console sonata:easy-extends:generate SonataUserBundle --dest=src --namespace_prefix=App
我的Bundles.php文件:
return [
Application\Sonata\UserBundle\ApplicationSonataUserBundle::class => ['all' => true],
];
Composer.json:
"autoload": {
"psr-4": {
"App\\": "src/",
"Application\\":"src/Application/"
}
},
doctrine.yaml
orm:
auto_generate_proxy_classes: true
entity_managers:
default:
naming_strategy: doctrine.orm.naming_strategy.underscore
auto_mapping: true
mappings:
SonataUserBundle: ~
FOSUserBundle: ~
ApplicationSonataUserBundle: ~
ApplicationSonataUserBundle类:
<?php
namespace Application\Sonata\UserBundle;
use Symfony\Component\HttpKernel\Bundle\Bundle;
/**
* This file has been generated by the SonataEasyExtendsBundle.
*
* @link https://sonata-project.org/easy-extends
*
* References:
* @link http://symfony.com/doc/current/book/bundles.html
*/
class ApplicationSonataUserBundle extends Bundle
{
/**
* {@inheritdoc}
*/
public function getParent()
{
return 'SonataUserBundle';
}
}
"require": {
"php": "^7.1.3",
"ext-ctype": "*",
"ext-iconv": "*",
"friendsofsymfony/rest-bundle": "^2.5",
"nelmio/api-doc-bundle": "^3.4",
"sensio/framework-extra-bundle": "^5.1",
"sonata-project/admin-bundle": "^3.48",
"sonata-project/doctrine-orm-admin-bundle": "^3.8",
"sonata-project/user-bundle": "^4.3",
"symfony/acl-bundle": "^1.0",
"symfony/asset": "4.2.*",
"symfony/console": "4.2.*",
"symfony/dotenv": "4.2.*",
"symfony/expression-language": "4.2.*",
"symfony/flex": "^1.1",
"symfony/form": "4.2.*",
"symfony/framework-bundle": "4.2.*",
"symfony/monolog-bundle": "^3.1",
"symfony/orm-pack": "*",
"symfony/process": "4.2.*",
"symfony/security-bundle": "4.2.*",
"symfony/serializer-pack": "*",
"symfony/swiftmailer-bundle": "^3.1",
"symfony/translation": "4.2.*",
"symfony/twig-bundle": "4.2.*",
"symfony/validator": "4.2.*",
"symfony/web-link": "4.2.*",
"symfony/yaml": "4.2.*"
},
我只需要扩展SonataUserBundle,但是每次尝试这样做时,我都会遇到相同的问题。我在项目中使用Symfony4。扩展捆绑包后会出现异常。
对这个问题有任何想法吗?我之前曾发布过问题,但没有答案。
答案 0 :(得分:1)
解决问题。我刚刚在我的命名空间上添加了App:
namespace App\Application\Sonata\UserBundle;
use Symfony\Component\HttpKernel\Bundle\Bundle;
/**
* This file has been generated by the SonataEasyExtendsBundle.
*
* @link https://sonata-project.org/easy-extends
*
* References:
* @link http://symfony.com/doc/current/book/bundles.html
*/
class ApplicationSonataUserBundle extends Bundle
{
/**
* {@inheritdoc}
*/
public function getParent()
{
return 'SonataUserBundle';
}
}
而且在bundles.php中:
return [
App\Application\Sonata\UserBundle\ApplicationSonataUserBundle::class => ['all' => true],
];