Symfony 2 after registration do extra work

In this case, after a successful login, I need to update the user login time to the base table.

The user object currently implements UserInterfaceand everything is in order. I just want to add additional code to register the user's login time.

I was looking for a site and it seemed to me that I should use EventListener, etc. a little hard. Other lighter alternatives?

+2
source share
1 answer

You can implement Success Hander.

  • Write a class that implements AuthenticationSuccessHandlerInterface:
  • ( , , ).

, :

firewalls:
dev:
    pattern:  ^/(_(profiler|wdt)|css|images|js)/
    security: false

secured_area:
    pattern:   ^/
    anonymous: ~
    form_login:
        login_path:  login
        check_path:  login_check
        success_handler: some.service.id
    logout:
        path: logout
        target: /

symfony2 ( fail_handler).

onSecurityInteractiveLogin :

public function onSecurityInteractiveLogin(InteractiveLoginEvent $event) 
    {
        $user = $event->getAuthenticationToken()->getUser();

        $user->setLastLogin(new \Datetime());
        // Entity manager injected by container in the service definition
        $this->em->persist($user); 
        $this->em->flush();


    }

+2

All Articles