如何覆盖fos用户注册控制器symfony 3.4

时间:2018-11-22 09:49:55

标签: fosuserbundle symfony-3.4 user-registration

我想知道如何正确逐步覆盖symfony 3.4上的FosUser Registration控制器。我认为该文档不是最新的。 我尝试了几种方法来执行此操作,但是它不起作用...这是我的最后尝试...

首先,路由:

ma_fos_user_registration:
resource: "@MAUserBundle/Resources/config/routing/override/fos.yml"
prefix:   /

fos_user:
resource: "@FOSUserBundle/Resources/config/routing/all.xml"

然后,在我的App目录中,这是我的替代控制器:

class RegistrationController extends BaseController
{
private $eventDispatcher;
private $formFactory;
private $userManager;
private $tokenStorage;

 public function __construct(EventDispatcherInterface $eventDispatcher, FactoryInterface $formFactory, UserManagerInterface $userManager, TokenStorageInterface $tokenStorage)
{
    parent::__construct($eventDispatcher, $formFactory, $userManager, $tokenStorage);

    $this->eventDispatcher = $eventDispatcher;
    $this->formFactory = $formFactory;
    $this->userManager = $userManager;
    $this->tokenStorage = $tokenStorage;
}

/**
 * @param Request $request
 *
 * @return Response
 */
public function registerAction(Request $request)
{

    //$response = parent::registerAction($request);

    //dump($response);die();
    // do custom stuff
    $user = $this->userManager->createUser();
    $user->setEnabled(true);
    $user->addRole('ROLE_USER');// --> I want to add the ROLE_ADMIN for example.
    $event = new GetResponseUserEvent($user, $request);
    $this->eventDispatcher->dispatch(FOSUserEvents::REGISTRATION_INITIALIZE, $event);

    if (null !== $event->getResponse()) {
        return $event->getResponse();
    }

    $form = $this->formFactory->createForm();
    $form->setData($user);

    $form->handleRequest($request);
    //dump($form);die();
    if ($form->isSubmitted()) {
        if ($form->isValid()) {
            dump($form);die();
            $event = new FormEvent($form, $request);
            $this->eventDispatcher->dispatch(FOSUserEvents::REGISTRATION_SUCCESS, $event);

            $this->userManager->updateUser($user);

            if (null === $response = $event->getResponse()) {
                $url = $this->generateUrl('fos_user_registration_confirmed');
                $response = new RedirectResponse($url);
            }

            $this->eventDispatcher->dispatch(FOSUserEvents::REGISTRATION_COMPLETED, new FilterUserResponseEvent($user, $request, $response));

            return $response;
        }

        $event = new FormEvent($form, $request);
        $this->eventDispatcher->dispatch(FOSUserEvents::REGISTRATION_FAILURE, $event);

        if (null !== $response = $event->getResponse()) {
            return $response;
        }
    }

    //return $response;


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

...//Other function
}

例如,我尝试为某些用户添加ROLE_ADMIN,但是当我转储检查时,我的控制器将被忽略?为什么?

0 个答案:

没有答案