用户登录Symfony 2.3后执行功能 [英] Execute function after User Login Symfony 2.3

查看:61
本文介绍了用户登录Symfony 2.3后执行功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在用户登录FOSUserBundle后执行功能,在我的config.yml中设置服务:

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 }

然后,我使用以下方法创建Listener类:

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.

请帮忙吗?

推荐答案

好吧,经过几个小时的搜寻并享受了尤文图斯对皇家马德里(2-1)的胜利,我终于找到了解决方案.我的解决方案包括修改security.yml中的'success_handler'并创建事件,这是代码:

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

然后在services.yml中声明服务:

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...

还是谢谢!

这篇关于用户登录Symfony 2.3后执行功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆