Symfony Fosuserbundle覆盖事件侦听器

Symfony Fosuserbundle覆盖事件侦听器,symfony,fosuserbundle,Symfony,Fosuserbundle,我正在尝试覆盖LastLoginListener以向其添加功能 一,;I’我正试着按描述做 看来 在AppBundle\DependencyInjection\OverrideServiceCompilerPass.php中 <?php namespace AppBundle\DependencyInjection\Compiler; use AppBundle\EventListener\LastLoginListener; use Symfony\Component\Depende

我正在尝试覆盖LastLoginListener以向其添加功能

一,;I’我正试着按描述做 看来

在AppBundle\DependencyInjection\OverrideServiceCompilerPass.php中

<?php

namespace AppBundle\DependencyInjection\Compiler;

use AppBundle\EventListener\LastLoginListener;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;

class OverrideServiceCompilerPass implements CompilerPassInterface
{
    public function process(ContainerBuilder $container)
    {
        $definition = $container->getDefinition('"fos_user.security.interactive_login_listener');
        $definition->setClass(LastLoginListener::class);
    }
侦听器本身是从包中复制的

自动加载程序需要在文件“/vendor/composer/./../src/AppBundle/DependencyInjection/OverrideServiceCompilerPass.php”中定义类“AppBundle\DependencyInjection\OverrideServiceCompilerPass”。找到了文件,但类不在其中,类名或命名空间可能有输入错误。 在DebugClassLoader.php中(第261行)

我的目标是添加上次使用侦听器登录的ip地址,但我需要创建另一个来添加角色和注册日期
我试图用“正确的方式”来做,而不是做一些粗俗的事情

最好使用
success\u handler
failure\u handler
服务

# app/config/security.yml
firewalls:
    main:
        ...
        form_login:
            ...
            success_handler: authentication_success_handler
            failure_handler: authentication_failure_handler
接下来,您需要注册您的服务并添加符合您需要的参数(可能是
@router
@doctor.orm.entity\u manager

然后您需要创建您的服务

// src/AppBundle/Handler/AuthenticationSuccessHandler.php
<?php

namespace AppBundle\Handler;


use Symfony\Component\Security\Http\Authentication\AuthenticationSuccessHandlerInterface;
use Symfony\Component\Security\Core\Exception\AuthenticationException;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Router;
use Doctrine\Common\Persistence\ObjectManager;

class AuthenticationSuccessHandler implements AuthenticationSuccessHandlerInterface {

    protected $router;
    private $em;

    public function __construct(Router $router, ObjectManager $em) {
        $this->router = $router;
        $this->em = $om;
    }

    public function onAuthenticationSuccess(Request $request, AuthenticationException $exception) {
        // your code here - creating new object. redirects etc.
    }

}
//src/AppBundle/Handler/AuthenticationSuccessHandler.php

我现在正在使用一个工作事件侦听器执行此操作,似乎不需要使用编译器传递来覆盖服务,我可以使用它来扩展默认侦听器的默认功能。很抱歉,我无法编辑我的评论,将其作为解决方案发布,我将接受。
# app/config/services.yml
authentication_success_handler:
    class: AppBundle\Handler\AuthenticationSuccessHandler
    arguments: ['@router', '@doctrine.orm.entity_manager']

authentication_failure_handler:
    class: AppBundle\Handler\AuthenticationFailureHandler
    arguments: ['@router', '@doctrine.orm.entity_manager']
// src/AppBundle/Handler/AuthenticationSuccessHandler.php
<?php

namespace AppBundle\Handler;


use Symfony\Component\Security\Http\Authentication\AuthenticationSuccessHandlerInterface;
use Symfony\Component\Security\Core\Exception\AuthenticationException;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Router;
use Doctrine\Common\Persistence\ObjectManager;

class AuthenticationSuccessHandler implements AuthenticationSuccessHandlerInterface {

    protected $router;
    private $em;

    public function __construct(Router $router, ObjectManager $em) {
        $this->router = $router;
        $this->em = $om;
    }

    public function onAuthenticationSuccess(Request $request, AuthenticationException $exception) {
        // your code here - creating new object. redirects etc.
    }

}
// src/AppBundle/Handler/AuthenticationFailureHandler.php
<?php

namespace AppBundle\Handler;

use Symfony\Component\Security\Http\Authentication\AuthenticationFailureHandlerInterface;
use Symfony\Component\Security\Core\Exception\AuthenticationException;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Router;
use Doctrine\Common\Persistence\ObjectManager;

class AuthenticationFailureHandler implements AuthenticationFailureHandlerInterface {

    protected $router;
    private $em;

    public function __construct(Router $router, ObjectManager $em) {
        $this->router = $router;
        $this->em = $om;
    }

    public function onAuthenticationFailure(Request $request, AuthenticationException $exception) {
        // your code here - creating new object. redirects etc.
    }

}