Symfony3在身份验证成功后将登录时间戳保存在数据库中


Symfony3 save login Timestamp in database after authentication success

好吧,标题说明了一切。我有一个很好用的登录表。但我要做的是保存用户成功登录时的时间戳,以及一个存储数据库记录更改时时间戳的字段。所以这是两个不同的领域。我在这里找到了这个答案,这正是我想要的。不幸的是,类Containerware在Symfony3中不存在,所以我不知道如何将EntityManager实例获取到我的处理程序中。我很确定,有一个简单的解决方案,但我就是找不到

在@micguo的提示下,我又前进了一步。这是我到目前为止的代码:

use Symfony'Component'Security'Http'Authentication'AuthenticationSuccessHandlerInterface;
use Symfony'Component'DependencyInjection'ContainerAwareTrait;
use Symfony'Component'HttpFoundation'Request;
use Symfony'Component'Security'Core'Authentication'Token'TokenInterface;
use Symfony'Component'HttpFoundation'RedirectResponse;
class AuthenticationHandler implements AuthenticationSuccessHandlerInterface{
use ContainerAwareTrait;
/**
 * {@inheritDoc}
 * @see 'Symfony'Component'Security'Http'Authentication'AuthenticationSuccessHandlerInterface::onAuthenticationSuccess()
 */
public function onAuthenticationSuccess(Request $request, TokenInterface $token) {
    $token->getUser()->setAnmeldat(new 'DateTime("now"));
    $this->container->get('doctrine')->getEntityManager()->flush();
    
    return new RedirectResponse($this->container->get('router')->generate('/auth/bnme'));
}
}

但当我尝试登录时,Symfony抛出了一个异常:

错误:在非对象上调用成员函数get()。

这里怎么了?

使用ContainerwareTrait代替


要使用ContainerwareTrait,您需要在services.yml文件中进行定义。

创建实现ContainerwareInterface 的服务