我有一个文件“AuthenticationHandler.php”,它使用“onAuthenticationFailure()”函数,可以在5次尝试失败后锁定帐户。如果帐户被锁定,则会向该用户发送一封电子邮件,其中包含指向其帐户的解锁链接...此处一切正常。
问题是当我尝试使用“render()”而不是简单地在“setBody(”something“,”text / html“)中添加句子时”
我尝试在我的AuthenticationHandler中注入@ twig / @ templating / @ service_container,但没有一个能够工作。当我故意登录失败时,我没有收到任何错误,也没有任何闪存包说“Bad Credential”。它什么都不做。它返回到登录页面,没有任何消息或邮件。
我知道我的导入(使用)是好的,因为如果我省略它们,我会收到一些常见的错误,它说我忘了使用声明。
我试过把一个try / catch放在“render()”周围,但是当我到达“$ this-> template-> render('email:myTestPage.html)时,它只是忽略了我的其余代码。树枝')
同样,我注入的所有其他服务都可以正常工作,如果我只是不使用“render()”函数,则会正确发送电子邮件。有没有其他方法来捕获错误消息?或者至少可以让我走上正轨!
service.yml:
authentication_handler:
class: Dbm\UserBundle\Handler\AuthenticationHandler
arguments: ["@session", "@router", "@security.context", "@doctrine.orm.entity_manager", "@mailer", "@twig"]
AuthenticationHandler.php
use Symfony\Component\Security\Http\Authentication\AuthenticationFailureHandlerInterface;
use Symfony\Component\Security\Http\Authentication\AuthenticationSuccessHandlerInterface;
use Symfony\Component\Security\Core\Exception\AuthenticationException;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\Security\Core\Exception\BadCredentialsException;
use Symfony\Component\Security\Core\SecurityContext;
use Symfony\Component\Routing\Router;
use Doctrine\ORM\EntityManager;
use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface;
class AuthenticationHandler implements AuthenticationSuccessHandlerInterface, AuthenticationFailureHandlerInterface
{
private $session;
private $security;
private $router;
private $em;
private $mailer;
private $twig;
public function __construct(Session $session, Router $router, SecurityContext $security, EntityManager $em, \Swift_Mailer $mailer, \Twig_Environment $twig)
{
$this->session = $session;
$this->security = $security;
$this->router = $router;
$this->em = $em;
$this->mailer = $mailer;
$this->twig = $twig;
}
public function onAuthenticationFailure(Request $request, AuthenticationException $exception)
{
...
$message = (new \Swift_Message())
->setSubject('Unlock Account')
->setFrom('foobar@foobar.com')
->setTo($mail)
->setBody($this->twig->render('Email:new-unexpire-account.html.twig', array('hash' => $hash)), 'text/html');
$this->mailer->send($message);
}
}
我也尝试过$ foo = $ this-> twig-> render('Email:new-unexpire-account.html.twig',array('hash'=> $ hash)),' text / html')单独,我也没有得到任何东西。它停在那里。