ric*_*age 10
对于问题的第一部分,我有类似的东西(例如存储用户登录的最后日期和时间).我走了一条服务的路线,这是一个事件被解雇了.在您的服务配置中(此处为XML示例):
<services>
<service id="my.login.listener" class="My\OwnBundle\Event\LoginEventListener">
<tag name="kernel.event_listener" event="security.interactive_login" />
</service>
</services>
Run Code Online (Sandbox Code Playgroud)
然后在您的包中的适当位置创建上述类:
namespace My\OwnBundle\Event;
use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;
use My\OwnBundle\User\User as MyUser;
class LoginEventListener
{
/**
* Catches the login of a user and does something with it
*
* @param \Symfony\Component\Security\Http\Event\InteractiveLoginEvent $event
* @return void
*/
public function onSecurityInteractiveLogin(InteractiveLoginEvent $event)
{
$token = $event->getAuthenticationToken();
if ($token && $token->getUser() instanceof MyUser)
{
// You can do something here eg
// record the date & time of the user's login
}
}
}
Run Code Online (Sandbox Code Playgroud)
我想你可以把它扩展到问题的第二部分,但是我没有这样做:-)