重置密码

时间:2017-04-17 20:51:58

标签: php symfony fosuserbundle

我正在使用Fosuserbundle,我希望在重置密码后呈现新模板包含成功的消息。但我不知道如何覆盖ResettingController文件以呈现模板。这是控制器:

public function resetAction(Request $request, $token)
{
    /** @var $formFactory \FOS\UserBundle\Form\Factory\FactoryInterface */
    $formFactory = $this->get('fos_user.resetting.form.factory');
    /** @var $userManager \FOS\UserBundle\Model\UserManagerInterface */
    $userManager = $this->get('fos_user.user_manager');
    /** @var $dispatcher \Symfony\Component\EventDispatcher\EventDispatcherInterface */
    $dispatcher = $this->get('event_dispatcher');

    $user = $userManager->findUserByConfirmationToken($token);

    if (null === $user) {
        throw new NotFoundHttpException(sprintf('The user with "confirmation token" does not exist for value "%s"', $token));
    }

    $form = $formFactory->createForm();

    $form->setData($user);

    $form->handleRequest($request);
    $form->getErrors(true);



    if ($form->isValid()) {
        $event = new FormEvent($form, $request);
        $dispatcher->dispatch(FOSUserEvents::RESETTING_RESET_SUCCESS, $event);
        $this->render('FOSUserBundle:Resetting:reset.html.twig', array(
            'token' => $token,
            'form' => $form->createView(),
        ));

        $userManager->updateUser($user);

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

        $dispatcher->dispatch(FOSUserEvents::RESETTING_RESET_COMPLETED, new FilterUserResponseEvent($user, $request, $response));

        return $response;
    }

    return $this->render('FOSUserBundle:Resetting:reset.html.twig', array(
        'token' => $token,
        'form' => $form->createView(),
    ));
}

1 个答案:

答案 0 :(得分:0)

我没有使用FosUserBundle,我不确定你的代码片段是否来自Fos控制器。在您的情况下,我认为您不需要覆盖控制器。只需听取FOSUserEvents::RESETTING_RESET_SUCCESS事件,创建自己的回复并回到事件$event->setResponse($response),您的响应将由Fos控制器使用。创建响应时,您可以根据需要使用任何参数渲染任何模板路径。

以下是一个例子:

class ResettingListener implements EventSubscriberInterface
{
    /**
     * @var EngineInterface
     */
    private $templating;

    public function __construct(EngineInterface $templating)
    {
        $this->templating = $templating;
    }

    /**
     * {@inheritdoc}
     */
    public static function getSubscribedEvents()
    {
        return [FOSUserEvents::RESETTING_RESET_SUCCESS => 'onResettingResetSuccess'];
    }

    /**
     * @param FormEvent $event
     */
    public function onResettingResetSuccess(FormEvent $event)
    {
        $response = $this->templating->render('YourBundle:Location:resetting.html.twig', [
            'form' => $event->getForm()->createView()
        ]);

        $event->setResponse($response);
    }
}

注册为服务:

<service id="your_bundle.listener.resetting" class="Vendor\YourBundle\EventListener\ResettingListener">
    <argument type="service" id="templating" />
    <tag name="kernel.event_subscriber" />
</service>