用户登录后调用方法

Hus*_*zal 7 symfony fosuserbundle

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

这是我想要调用的代码:

$point = $this->container->get('process_points');
$point->ProcessPoints(1 , $this->container);
Run Code Online (Sandbox Code Playgroud)

Pie*_*eau 28

您可以在FOSUserEvents类中找到FOSUserBundle触发的事件.更具体地说,这是您正在寻找的那个:

/**
 * 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';
Run Code Online (Sandbox Code Playgroud)

可以在Hooking into controllers doc页面中找到用于挂钩这些事件文档.在您的情况下,您将需要实现这样的事情:

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);
    }
}
Run Code Online (Sandbox Code Playgroud)

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

services:
    acme_user.login:
        class: Acme\UserBundle\EventListener\LoginListener
        arguments: [@container]
        tags:
            - { name: kernel.event_subscriber }
Run Code Online (Sandbox Code Playgroud)

还有另一种涉及覆盖控制器的方法,但是如文档中所述,您必须复制它们的代码,因此它不是完全干净的,并且如果(或者更确切地说,何时)FOSUserBundle被更改,则必然会中断.