最近,我开始使用Symfony2。现在我想在我的网站上添加一个用户管理引擎。
但我面临着一个问题。这就是我正在做的:
-
在创建/安装基本Symfony2项目方面:
$ composer create-project symfony/framework-standard-edition path/ "2.5.*" $ mv path/* ./ $ rm -r path/
-
好的,Symfony 2.5.5到此为止。接下来,下载FOSUserBundle并创建一个自定义捆绑包:
$ composer require friendsofsymfony/user-bundle '~2.0@dev' $ php app/console generate:bundle --namespace=Meiblorn/CoreBundle --format=yml
-
在Meiborn\CoreBundle\Framework\Domain命名空间中创建User类
/** * User: Meiblorn * Date: 15/10/14 * Time: 20:17 */ namespace MeiblornCoreBundleFrameworkDomain; use FOSUserBundleModelUser as FOSUserBundleUser; use DoctrineORMMapping as ORM; /** * @ORMEntity * @ORMTable( * name = "users" * ) */ class User extends FOSUserBundleUser { /** * @ORMId * @ORMColumn(type="integer") * @ORMGeneratedValue(strategy="AUTO") */ protected $id; public function __construct() { parent::__construct(); // your own logic } } ?>
-
配置security.yml和config.yml。最后,我得到了这个:
AppKernel.php
$bundles = array( new SymfonyBundleFrameworkBundleFrameworkBundle(), new SymfonyBundleSecurityBundleSecurityBundle(), new SymfonyBundleTwigBundleTwigBundle(), new SymfonyBundleMonologBundleMonologBundle(), new SymfonyBundleSwiftmailerBundleSwiftmailerBundle(), new SymfonyBundleAsseticBundleAsseticBundle(), new DoctrineBundleDoctrineBundleDoctrineBundle(), new FOSUserBundleFOSUserBundle(), new MeiblornCoreBundleMeiblornCoreBundle(), );
config.yml
imports: - { resource: parameters.yml } - { resource: security.yml } framework: #esi: ~ translator: { fallback: "%locale%" } secret: "%secret%" router: resource: "%kernel.root_dir%/config/routing.yml" strict_requirements: ~ form: ~ csrf_protection: ~ validation: { enable_annotations: true } templating: engines: ['twig'] #assets_version: SomeVersionScheme default_locale: "%locale%" trusted_hosts: ~ trusted_proxies: ~ session: # handler_id set to null will use default session handler from php.ini handler_id: ~ fragments: ~ http_method_override: true # Twig Configuration twig: debug: "%kernel.debug%" strict_variables: "%kernel.debug%" # Assetic Configuration assetic: debug: "%kernel.debug%" use_controller: false bundles: [ ] #java: /usr/bin/java filters: cssrewrite: ~ #closure: # jar: "%kernel.root_dir%/Resources/java/compiler.jar" #yui_css: # jar: "%kernel.root_dir%/Resources/java/yuicompressor-2.4.7.jar" # Doctrine Configuration doctrine: dbal: driver: "%database_driver%" host: "%database_host%" port: "%database_port%" dbname: "%database_name%" user: "%database_user%" password: "%database_password%" charset: UTF8 orm: auto_generate_proxy_classes: "%kernel.debug%" auto_mapping: true # Swiftmailer Configuration swiftmailer: transport: "%mailer_transport%" host: "%mailer_host%" username: "%mailer_user%" password: "%mailer_password%" spool: { type: memory } fos_user: db_driver: orm firewall_name: prod user_class: MeiblornCoreBundleFrameworkDomainUser
security.yml
security: encoders: FOSUserBundleModelUserInterface: sha512 role_hierarchy: ROLE_ADMIN: ROLE_USER ROLE_SUPER_ADMIN: ROLE_ADMIN providers: fos_userbundle: id: fos_user.user_provider.username firewalls: prod: pattern: ^/ form_login: provider: fos_userbundle csrf_provider: form.csrf_provider logout: true anonymous: true dev: pattern: ^/(_(profiler|wdt)|css|images|js)/ security: false access_control: - { path: ^/login$, role: IS_AUTHENTICATED_ANONYMOUSLY } - { path: ^/register, role: IS_AUTHENTICATED_ANONYMOUSLY } - { path: ^/resetting, role: IS_AUTHENTICATED_ANONYMOUSLY } - { path: ^/admin/, role: ROLE_ADMIN }
这就是问题所在
In browser: http://localhost/test.meiblorn.com/web/app_dev.php/
MappingException: The class 'MeiblornCoreBundleFrameworkDomainUser' was not found in the chain configured namespaces FOSUserBundleModel
in /Library/WebServer/Documents/test.meiblorn.com/vendor/doctrine/common/lib/Doctrine/Common/Persistence/Mapping/MappingException.php line 37
at MappingException::classNotFoundInNamespaces('MeiblornCoreBundleFrameworkDomainUser', array('FOSUserBundleModel')) in /Library/WebServer/Documents/test.meiblorn.com/vendor/doctrine/common/lib/Doctrine/Common/Persistence/Mapping/Driver/MappingDriverChain.php line 113
at MappingDriverChain->loadMetadataForClass('MeiblornCoreBundleFrameworkDomainUser', object(ClassMetadata)) in /Library/WebServer/Documents/test.meiblorn.com/vendor/doctrine/orm/lib/Doctrine/ORM/Mapping/ClassMetadataFactory.php line 117
at ClassMetadataFactory->doLoadMetadata(object(ClassMetadata), object(ClassMetadata), false, array()) in /Library/WebServer/Documents/test.meiblorn.com/vendor/doctrine/common/lib/Doctrine/Common/Persistence/Mapping/AbstractClassMetadataFactory.php line 318
此外,Doctrine在调用Doctrine:schema:update时不会为此映射创建表
请帮我解决这个异常
更新!如何修复
我的命名空间的最终配置
orm:
auto_generate_proxy_classes: "%kernel.debug%"
auto_mapping: false
mappings:
FOSUserBundle: ~
MeiblornCoreBundle:
type: annotation
dir: %kernel.root_dir%/../src/Meiblorn/CoreBundle/Framework/Entity
prefix: MeiblornCoreBundleFrameworkEntity
# alias: MyModels
# is_bundle: true
首先需要在composer.js中配置psr-4自动加载,例如
"autoload": {
"psr-4": {
"Meiblorn\CoreBundle\": "src/Meiblorn/CoreBundle/"
}
},
然后调用composer dumpautoload
。
其次,我相信Doctrine希望实体位于文件夹Entity/
中,所以尝试将您的模型从src/Meiblorn/CoreBundle/Framework/Domain/User.php
移动到src/Meiblorn/CoreBundle/Entity/User.php
,或者我如何更改symfony 2 Doctrine映射器以使用我的自定义目录,而不是捆绑包下的实体目录