问题
I have an event listener that should fire when a user logs in in my Symfony setup.
I have the following in my services.yml
...
d_user.login_listener:
class: D\UserBundle\EventListener\LoginListener
arguments: []
tags:
- { name: 'kernel.event_subscriber', event: 'security.interactive_login' }
and in my login listener I simply have this:
<?php
namespace D\UserBundle\EventListener;
use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class LoginListener implements EventSubscriberInterface
{
public function onSecurityInteractiveLogin(InteractiveLoginEvent $event)
{
echo 'user logged in';
}
public static function getSubscribedEvents()
{
return array(
// must be registered before the default Locale listener
'security.interactive_login' => array(array('onSecurityInteractiveLogin', 18))
);
}
}
On my dev server I correctly see the text "user logged in" before redirection, but on my production server it just logs in without the event firing. I'm modifying this later to set a session var on user login. I just need to get the method called.
Any advice? I've tried clearing the cache for prod and dev on my production environment, but that didn't help.
回答1:
Debugging with echo is discouraged. If you want an output, use the logger!
namespace D\UserBundle\EventListener;
use Psr\Log\LoggerInterface;
use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class LoginListener implements EventSubscriberInterface
{
private $logger;
private $router;
public function __construct(LoggerInterface $logger)
{
$this->logger = $logger;
}
public function onSecurityInteractiveLogin(InteractiveLoginEvent $event)
{
$this->logger->info('user logged in');
}
public static function getSubscribedEvents()
{
return array(
// must be registered before the default Locale listener
'security.interactive_login' => array(array('onSecurityInteractiveLogin', 18))
);
}
}
and inject the logger in your service defintion, preferrable with a nice channel name:
d_user.login_listener:
class: D\UserBundle\EventListener\LoginListener
arguments: [@logger]
tags:
- { name: 'kernel.event_subscriber', event: 'security.interactive_login' }
- { name: monolog.logger, channel: d_user }
来源:https://stackoverflow.com/questions/18827681/login-eventlistener-not-firing-on-production-side