0

我在路径中创建了一个名为 AccountBundle 的 Bundle /src/EF/Bundle/。这会覆盖 FOSUserBundle 进行自定义。

namespace EF\Bundle\AccountBundle;

use Symfony\Component\HttpKernel\Bundle\Bundle;

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

然后,在路径 AccountBundle/Form/Type 中,我试图覆盖 RegistrationType

namespace EF\Bundle\AccountBundle\Form\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use FOS\UserBundle\Form\Type\RegistrationFormType as BaseRegistrationFormType;

class UserRegistrationFormType extends BaseRegistrationFormType
{
    private $class;

    /**
     * @param string $class The User class name
     */
    public function __construct($class)
    {
        $this->class = $class;
    }

    /**
     * @param object $builder FormBuilderInterface
     * @param array $options Options that override FOSUserBundle
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        parent::buildForm($builder, $options);

        // Adding custom Fields to override FOS User bundle
        $builder
            ->add('firstname', null, array('label' => 'fos.username', 'translation_domain' => 'FOSUserBundle'))
        ;
    }

    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => $this->class,
            'intention'  => 'registration',
        ));
    }

    public function getName()
    {
        return 'ef_account_registration';
    }
}

在 AccountBundle/Resources/config 上的 services.xml 上添加了一项服务

<!-- src/FOS/UserBundle/Resources/config/registration.xml -->
<?xml version="1.0" encoding="UTF-8" ?>

<container xmlns="http://symfony.com/schema/dic/services"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">

<services>

       <service id="fos_user.registration.form.type" class="EF\Bundle\AccountBundle\Form\Type\UserRegistrationFormType">
            <tag name="form.type" alias="ef_account_registration" />
            <argument>%fos_user.model.user.class%</argument>
        </service>
  </services>

</container>

然后在配置文件中进行更改,/app/config/config.yml 为

fos_user:
    db_driver: propel # other valid values are 'mongodb', 'couchdb' and 'propel'
    firewall_name: main
    user_class: MH\Security\User
    #user_class: FOS\UserBundle\Propel\User
    registration:
        confirmation:
            enabled: true
        form:
            type: ef_account_registration

但是我在尝试注册时遇到错误,

无法加载类型“ef_account_registration”500 内部服务器错误 - FormException

但是,如果我更改ef_account_registrationwith fos_user_registration,那么它可以工作。任何人都可以在这方面帮助我吗?

4

1 回答 1

0

你可以删除这部分:

form:
            type: ef_account_registration

我认为在 UserRegistrationFormType 中您只需要 buildForm 方法,所有其他方法都可以删除并在您的服务中(我使用 yml):

fos_user.registration.form.type:
        class: YOUR_CLASS
        tags:
             - { name: 'form.type', alias: 'fos_user_registration' }
        arguments:
             - %fos_user.model.user.class%

它对我有用

于 2013-04-17T11:00:12.573 回答