用户登录后调用方法 [英] Call a method after user Login

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

问题描述

我想知道用户登录后是否可以调用函数.

I was wondering if there is away to call a function after the user login.

这是我要调用的代码:

$point = $this->container->get('process_points');
$point->ProcessPoints(1 , $this->container);

推荐答案

您可以在

You can find the events FOSUserBundle fires in the FOSUserEvents class. More specifically, this is the one you are looking for:

/**
 * The SECURITY_IMPLICIT_LOGIN event occurs when the user is logged in programmatically.
 *
 * This event allows you to access the response which will be sent.
 * The event listener method receives a FOS\UserBundle\Event\UserEvent instance.
 */
const SECURITY_IMPLICIT_LOGIN = 'fos_user.security.implicit_login';

与这些事件相关的文档可以在连接到控制器中找到文档页面.在您的情况下,您将需要实现以下内容:

The documentation for hooking into those events can be found on the Hooking into the controllers doc page. In your case, you will need to implement something like this:

namespace Acme\UserBundle\EventListener;

use FOS\UserBundle\FOSUserEvents;
use FOS\UserBundle\Event\FormEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\Security\Http\SecurityEvents;
use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;

/**
 * Listener responsible to change the redirection at the end of the password resetting
 */
class LoginListener implements EventSubscriberInterface
{
    private $container;

    public function __construct($container)
    {
        $this->container = $container;
    }

    /**
     * {@inheritDoc}
     */
    public static function getSubscribedEvents()
    {
        return array(
            FOSUserEvents::SECURITY_IMPLICIT_LOGIN => 'onLogin',
            SecurityEvents::INTERACTIVE_LOGIN => 'onLogin',
        );
    }

    public function onLogin($event)
    {
        // FYI
        // if ($event instanceof UserEvent) {
        //    $user = $event->getUser();
        // }
        // if ($event instanceof InteractiveLoginEvent) {
        //    $user = $event->getAuthenticationToken()->getUser();
        // }

        $point = $this->container->get('process_points');
        $point->ProcessPoints(1 , $this->container);
    }
}

然后应将侦听器定义为服务并注入容器.另外,您可以只注入所需的服务,而不注入整个容器.

You should then define the listener as a service and inject the container. Alternatively, you could inject just the service you need instead of the whole container.

services:
    acme_user.login:
        class: Acme\UserBundle\EventListener\LoginListener
        arguments: [@container]
        tags:
            - { name: kernel.event_subscriber }

还有另一种方法,涉及覆盖控制器,但是正如文档中所述,您必须复制它们的代码,因此如果FOSUserBundle发生更改(或更确切地说,何时更改),它就不是很干净并且肯定会中断.​​

There is also another method which involves overriding the controller, but as noted in the documentation, you have to duplicate their code so it's not exactly clean and bound to break if (or rather, when) FOSUserBundle is changed.

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

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