src/EventListener/UserActivityListener.php
public function onCoreController(FilterControllerEvent $event)
    {
        // do not capture admin cms urls
        if (!preg_match('/.*\/admin\/.*/', $event->getRequest()->getRequestUri())) {
            return;
        }

        if (!$this->tokenStorage->getToken()) {
            return;
        }

        $user = $this->tokenStorage->getToken()->getUser();
        if ($user instanceof UserInterface) {

            //here we can update the user as necessary
            if (method_exists($user, 'setLastActivity')) {
                try {
                    $user->setLastActivity(new \DateTime('now'));
                    $this->em->persist($user);
                    $this->em->flush($user);
                } catch (\Doctrine\ORM\ORMException $e) {
                    //do nothing, entity manager is closed
                }
            }
        }
    }
public function onCoreController(FilterControllerEvent $event)
    {
        // do not capture admin cms urls
        if (!preg_match('/.*\/admin\/.*/', $event->getRequest()->getRequestUri())) {
            return;
        }

        if (!$this->tokenStorage->getToken()) {
            return;
        }

        $user = $this->tokenStorage->getToken()->getUser();
        if ($user instanceof UserInterface) {

            //here we can update the user as necessary
            if (method_exists($user, 'setLastActivity')) {
                try {
                    $user->setLastActivity(new \DateTime('now'));
                    $this->em->persist($user);
                    $this->em->flush($user);
                } catch (\Doctrine\ORM\ORMException $e) {
                    //do nothing, entity manager is closed
                }
            }
        }
    }
public function onCoreController ( FilterControllerEvent $ event ) { // do not capture admin cms urls if ( ! preg_match ( '/.*\/admin\/.*/' , $ event -> getRequest ( ) -> getRequestUri ( ) ) ) { return ; } if ( ! $ this -> tokenStorage -> getToken ( ) ) { return ; } $ user = $ this -> tokenStorage -> getToken ( ) -> getUser ( ) ; if ( $ user instanceof UserInterface ) { //here we can update the user as necessary if ( method_exists ( $ user , 'setLastActivity' ) ) { try { $ user -> setLastActivity ( new \ DateTime ( 'now' ) ) ; $ this -> em -> persist ( $ user ) ; $ this -> em -> flush ( $ user ) ; } catch ( \ Doctrine \ ORM \ ORMException $ e ) { //do nothing, entity manager is closed } } } }
On each request we want to update the user's last activity datetime.

@param \Symfony\Component\HttpKernel\Event\FilterControllerEvent $event
On each request we want to update the user s last activity datetime .