重置密码后呈现新模板包含成功消息
Render new template contain successfully message after resetting password
我正在使用 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(),
));
}
我没有使用 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>
我正在使用 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(),
));
}
我没有使用 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>