【发布时间】:2017-11-27 13:33:29
【问题描述】:
我不习惯寻求帮助,但是对于这个问题我承认我自己解决不了。
我在同一页面上有 2 个表单(来自 fosuserbundle 的登录表单和注册表单) 两种形式都有效。
当现有用户尝试注册时会出现我的问题。
我创建了一个RegistrationFailureListener,如果电子邮件已经存在(例如),他会在同一页面上重定向用户。
但是register/路由上显示的错误并没有显示在这个页面上,有两种形式。
我不知道如何在我的页面上发送这些表单错误。
对不起,我的英语!
我的代码:
<?php
namespace TNS\UserBundle\EventListener;
use FOS\UserBundle\Event\FormEvent;
use FOS\UserBundle\FOSUserEvents;
use FOS\UserBundle\Mailer\MailerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Security\Csrf\TokenGenerator\TokenGeneratorInterface;
class RegistrationListener implements EventSubscriberInterface
{
/**
* @var UrlGeneratorInterface
*/
private $router;
/**
* @var TokenGeneratorInterface
*/
private $tokenGenerator;
/**
* @var SessionInterface
*/
private $session;
/**
* @var MailerInterface
*/
private $mailer;
/**
* RegistrationSuccessListener constructor.
* @param UrlGeneratorInterface $router
* @param TokenGeneratorInterface $tokenGenerator
* @param SessionInterface $session
* @param MailerInterface $mailer
*/
public function __construct(UrlGeneratorInterface $router, TokenGeneratorInterface $tokenGenerator, SessionInterface $session, MailerInterface $mailer)
{
$this->router = $router;
$this->tokenGenerator = $tokenGenerator;
$this->session = $session;
$this->mailer = $mailer;
}
/**
* Returns an array of event names this subscriber wants to listen to.
*
* The array keys are event names and the value can be:
*
* * The method name to call (priority defaults to 0)
* * An array composed of the method name to call and the priority
* * An array of arrays composed of the method names to call and respective
* priorities, or 0 if unset
*
* For instance:
*
* * array('eventName' => 'methodName')
* * array('eventName' => array('methodName', $priority))
* * array('eventName' => array(array('methodName1', $priority), array('methodName2')))
*
* @return array The event names to listen to
*/
public static function getSubscribedEvents()
{
return array(
FOSUserEvents::REGISTRATION_SUCCESS => 'onRegistrationSuccess',
FOSUserEvents::REGISTRATION_FAILURE => 'onRegistrationFailure'
);
}
public function onRegistrationFailure( FormEvent $event ) {
$url = $this->router->generate('tns_core_homepage');
$event->setResponse(new RedirectResponse($url));
}
public function onRegistrationSuccess(FormEvent $event)
{
/** @var $user \FOS\UserBundle\Model\UserInterface */
$user = $event->getForm()->getData();
$user->setEnabled(false);
if (null === $user->getConfirmationToken()) {
$user->setConfirmationToken($this->tokenGenerator->generateToken());
}
$this->session->set('fos_user_send_confirmation_email/email', $user->getEmail());
$url = $this->router->generate('fos_user_registration_check_email');
$event->setResponse(new RedirectResponse($url));
}
}
我的RegisterController和fosuser registercontroller一样
【问题讨论】:
-
这里的答案建议渲染模板而不是重定向 - stackoverflow.com/questions/17269844/…
-
好的,我尝试在我的服务中注入模板,现在我进行渲染而不是重定向。但现在我被重定向到注册/路径而不是我的自定义页面,这不是我想要的。谢谢,我学会了在我的服务中注入模板
-
我是否在 registercontroller 中的 registeraction 结束时更改渲染?
标签: php symfony events fosuserbundle