Login EventListener not firing on production side

倖福魔咒の 提交于 2019-12-10 11:45:40

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!