Symfony FOS用户捆绑包注册表覆盖错误

时间:2015-09-16 13:01:05

标签: symfony fosuserbundle

我正在尝试覆盖fos用户包注册表单,但doc示例对我不起作用

我的services.yml

services:
    app.form.registration:
        class: AppBundle\Form\RegistrationFormType
        tags:
            - { name: form.type, alias: app_user_registration }
    app.locale_listener:
        class: AppBundle\EventListener\LocaleListener
        arguments: ["%kernel.default_locale%"]
        tags:
            - { name: kernel.event_subscriber }

路线

register:
    pattern: /register
    defaults: { _controller: AppBundle:Registration:register }

注册控制器

namespace AppBundle\Controller;

use Symfony\Component\HttpFoundation\RedirectResponse;
use FOS\UserBundle\Controller\RegistrationController as BaseController;
use Symfony\Component\HttpFoundation\Request;
use FOS\UserBundle\FOSUserEvents;
use FOS\UserBundle\Event\GetResponseUserEvent;

class RegistrationController extends BaseController
{
   public function registerAction(Request $request)
    {
        $form = $this->container->get('app.registration.form');
        $formHandler = $this->container->get('fos_user.registration.form.handler');
        $confirmationEnabled = $this->container->getParameter('fos_user.registration.confirmation.enabled');

        $process = $formHandler->process($confirmationEnabled);
        if ($process) {
            $user = $form->getData();

            /*****************************************************
             * Add new functionality (e.g. log the registration) *
             *****************************************************/
            $this->container->get('logger')->info(
                sprintf('New user registration: %s', $user)
            );

            if ($confirmationEnabled) {
                $this->container->get('session')->set('fos_user_send_confirmation_email/email', $user->getEmail());
                $route = 'fos_user_registration_check_email';
            } else {
                $this->authenticateUser($user);
                $route = 'fos_user_registration_confirmed';
            }

            $this->setFlash('fos_user_success', 'registration.flash.user_created');
            $url = $this->container->get('router')->generate($route);

            return new RedirectResponse($url);
        }

        return $this->render('blog/register.html.twig', array(
            'form' => $form->createView(),
        ));
    }
}

登记表格类型

namespace AppBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use FOS\UserBundle\Form\Type\RegistrationFormType as BaseController;

class RegistrationFormType extends BaseController
{
    /**
     * @param string $class The User class name
     */
    public function __construct($class)
    {
        parent::__construct($class);
    }

    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('username', null, array('label' => 'form.username', 'translation_domain' => 'FOSUserBundle'))
            ->add('plainPassword', 'repeated', array(
                'type' => 'password',
                'options' => array('translation_domain' => 'FOSUserBundle'),
                'first_options' => array('label' => 'form.password'),
                'second_options' => array('label' => 'form.password_confirmation'),
                'invalid_message' => 'fos_user.password.mismatch',
            ))
            ->add('email', 'email', array('label' => 'form.email', 'translation_domain' => 'FOSUserBundle'))
            ->add('phone', null, array('label' => 'form.phone', 'translation_domain' => 'FOSUserBundle'))
            ->add('gender', null, array(
                'choices' => array(
                'morning'   => 'Morning',
                'afternoon' => 'Afternoon',
                'evening'   => 'Evening',
                )
            ))
            ->add('about', null, array('label' => 'form.about', 'type' => 'textarea', 'translation_domain' => 'FOSUserBundle'))
        ;
    }
}

开启/注册页面即时收到错误 - 您已请求不存在的服务" app.registration.form"。

将默认注册表格覆盖到我的自定义的正确解决方案是什么?

谢谢

0 个答案:

没有答案