我有一个事件监听器,当用户登录我的Symfony设置时应该触发。
我的services.yml
中有以下内容...
d_user.login_listener:
class: D\UserBundle\EventListener\LoginListener
arguments: []
tags:
- { name: 'kernel.event_subscriber', event: 'security.interactive_login' }
在我的登录听众中,我只想拥有:
<?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))
);
}
}
在我的开发服务器上,我在重定向之前正确地看到了“用户登录”文本,但是在我的生产服务器上它只是在没有事件触发的情况下登录。我稍后会修改它以在用户登录时设置会话var。我只需要调用方法。
有什么建议吗?我已经尝试在我的生产环境中为prod和dev清除缓存,但这没有帮助。
答案 0 :(得分:2)
不鼓励使用echo进行调试。如果您想要输出,请使用记录器!
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))
);
}
}
并在您的服务定义中注入记录器,优先选择一个好的频道名称:
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 }