问题
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?
回答1:
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:
security:
..
firewalls:
main:
..
success_handler: authentication.success.listener
Then in services.yml I declare the service:
services:
authentication.success.listener:
class: MyCompany\MyBundle\EventListener\AuthenticationEventListener
arguments: ['@router', '@security.context', '@service_container']
Then I declare the class/function to listen:
// MyCompany\MyBundle\EventListener\AuthenticationEventListener.php
<?php
namespace MyCompany\MyBundle\EventListener;
use Symfony\Component\Security\Http\Authentication\AuthenticationSuccessHandlerInterface;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\SecurityContext;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Router;
class AuthenticationEventListener implements AuthenticationSuccessHandlerInterface
{
protected $router;
protected $security;
protected $container;
protected $em;
public function __construct(Router $router, SecurityContext $security, $container)
{
$this->router = $router;
$this->security = $security;
$this->container = $container;
$this->em = $this->container->get('doctrine')->getEntityManager();
}
public function onAuthenticationSuccess(Request $request, TokenInterface $token)
{
$response = new Response();
$response = new RedirectResponse('dashboard');
return $response;
}
}
?>
I hope it could be useful for you guys...
Thanks anyways!
回答2:
Try this code:
public static function getSubscribedEvents()
{
return array(
FOSUserEvents::SECURITY_IMPLICIT_LOGIN => array('onAuthenticationnSuccess',0)
);
}
This should be the syntax liked documented here: http://symfony.com/doc/current/components/event_dispatcher/introduction.html
来源:https://stackoverflow.com/questions/30054216/execute-function-after-user-login-symfony-2-3