FOSUserBundle覆盖注册但无法加载类型错误

时间:2017-01-24 09:30:23

标签: php symfony fosuserbundle

我尝试使用FOSUserBundle,我按照文档中的说明覆盖了Bundle,但是当我尝试访问/ register时/ login工作(我没有覆盖它)时出现此错误:

Could not load type "app_user_registration"
500 Internal Server Error - InvalidArgumentException 

配置

Symfony版本: 3.1.7

FOSUserBundle版本: dev-master

我的文件

应用程序/配置/ services.yml:

services:
    app.form.registration:
        class: CoreBundle\Form\Type\RegistrationFormType
        tags:
            - { name: form.type, alias: app_user_registration }

应用程序/配置/ config.yml:

fos_user:
    db_driver: orm
    firewall_name: main
    user_class: CoreBundle\Entity\User
    registration:
        form:
            type: app_user_registration

的src / CoreBundle /窗体/类型/ RegistrationFormType.php

<?php
// src/CoreBundle/Form/Type/RegistrationFormType.php

namespace CoreBundle\Form\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;

class RegistrationFormType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('name');
    }

    public function getParent()
    {
        return 'fos_user_registration';
    }

    public function getName()
    {
        return 'app_user_registration';
    }
}
?>

的src / viwa / UserBundle /控制器/ RegistrationController.php:

<?php
// src/viwa/UserBundle/Controller/RegistrationController.php

namespace viwa\UserBundle\Controller;

use Symfony\Component\HttpFoundation\RedirectResponse;
use FOS\UserBundle\Controller\RegistrationController as BaseController;

class RegistrationController extends BaseController
{
    // Don't need to change this right now.
}
?>

的src / viwa / UserBundle / viwaUserBundle.php:

<?php
// src/viwa/UserBundle/viwaUserBundle.php

namespace viwa\UserBundle;

use Symfony\Component\HttpKernel\Bundle\Bundle;

class viwaUserBundle extends Bundle
{
    public function getParent()
    {
        return 'FOSUserBundle';
    }
}

如果您还需要其他帮助我,我可以编辑我的帖子。

希望任何人都可以帮助我。

3 个答案:

答案 0 :(得分:5)

您的 config.yml 文件应为:

fos_user:
    db_driver: orm
    firewall_name: main
    user_class: CoreBundle\Entity\User
    registration:
        form:
            type: CoreBundle\Form\Type\RegistrationFormType

src/CoreBundle/Form/Type/RegistrationFormType.php中,getParent()函数应为:

public function getParent()
{
    return 'FOS\UserBundle\Form\Type\RegistrationFormType';
}

答案 1 :(得分:1)

您可能阅读了默认打开的“1.3.x / current”文档。 如果您切换到'2.0 / master',您将获得正确版本的文档。

答案 2 :(得分:0)

我知道这是一个古老的问题,但是当我发现它寻找相同的问题时,我会分享我的解决方案,就像已接受的答案一样,但更符合建议的/正式的语法:) 我已经升级到symfony 3.4和FosUserBundle 2.1,现在您需要按照UPGRADE 2.x to 3.0

中的说明返回FQCN。
  

从FormTypeInterface :: getParent()返回类型实例不是   支持了。返回父级的全限定类名   改为输入class。

     

之前:

class MyType
{
    public function getParent()
    {
        return new ParentType();
    }
}
  

之后:

class MyType
{
    public function getParent()
    {
        return ParentType::class;
    }
}

在这种情况下:

public function getParent()
{
    return RegistrationFormType::class;
}

别忘了在文件顶部使用:

use FOS\UserBundle\Form\Type\RegistrationFormType;
相关问题