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

FOSUserBundle overwrite registration but Could not load type error

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

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

配置

Symfony 版本:3.1.7

FOSUserBundle 版本:dev-master

我的文件

app/config/services.yml:

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

app/config/config.yml:

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

src/CoreBundle/Form/Type/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/Controller/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';
    }
}

如果你需要任何其他帮助我,我编辑我的post。

希望有人能帮帮我。

您的 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.3.x/current”的文档。 如果切换到“2.0 / master”,您将看到正确版本的文档。

我知道这是一个老问题,但当我发现它在搜索相同的问题时,我分享了我的解决方案,比如已接受的答案,但更符合 suggested/official 语法 :) 我已升级到 symfony 3.4 和 FosUserBundle 2.1,现在您需要 return FQCN,如 UPGRADE 2.x to 3.0

中所述

Returning type instances from FormTypeInterface::getParent() is not supported anymore. Return the fully-qualified class name of the parent type class instead.

Before:

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

After:

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

在这种情况下:

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

不要忘记文件顶部的用法:

use FOS\UserBundle\Form\Type\RegistrationFormType;