I am trying to execute a function after user login in FOSUserBundle, in my config.yml I set the service:
services:
authentication.success.listener:
class: MyCompany\MyBundle\EventListener\AuthenticationEventListener
arguments: [@router]
tags:
- { name: kernel.event_subscriber }
Then, I create the Listener class with the methods:
<?php
namespace MyCompany\MyBundle\EventListener;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use FOS\UserBundle\FOSUserEvents;
use FOS\UserBundle\Event\UserEvent;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Cookie as Cookie;
class AuthenticationEventListener implements EventSubscriberInterface
{
private $router;
public function __construct(UrlGeneratorInterface $router)
{
$this->router = $router;
}
public static function getSubscribedEvents()
{
return array(
FOSUserEvents::SECURITY_IMPLICIT_LOGIN => 'onAuthenticationSuccess',
);
}
public function onAuthenticationSuccess(UserEvent $event)
{
//my actions goes here...
}
}
?>
When I try to login, nothing happens after, I write some wrong code for generate an exception but everything goes well... apparently the function is not being excetuted.
Any help please?
Well, finally after few hours searching and enjoying the Juventus victory over Real Madrid (2-1) I found a solution. My solution consist in modify the 'success_handler' in security.yml and create the event, this is the code:
Then in services.yml I declare the service:
Then I declare the class/function to listen:
I hope it could be useful for you guys...
Thanks anyways!
Try this code:
This should be the syntax liked documented here: http://symfony.com/doc/current/components/event_dispatcher/introduction.html