2017-08-09 92 views
1

我试图覆盖LastLoginListener以向其添加功能。Fosuserbundle覆盖事件监听器

我;想要做它描述here 看来

在的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); 
    } 

services.yml

services: 
    app.login_listener: 
    class: AppBundle\EventListener\LastLoginListener 
    arguments: [] 
    tags: 
    - { name: kernel.event_subscriber } 

监听器本身从包中复制。

自动加载预期类 “的appbundle \ DependencyInjection \ OverrideServiceCompilerPass” 到文件 “/vendor/composer/../../src/AppBundle/DependencyInjection/OverrideServiceCompilerPass.php” 来定义。该文件已找到,但该类不在其中,类名或命名空间可能有拼写错误。 在DebugClassLoader.php(线路261)

我的目标是增加与听众的最后登录的IP地址,但我需要创建另一个添加一个角色和注册日期 我想做它“正确的方式”,而不是做一些事情hackish

回答

2

它更好地使用success_handlerfailure_handler服务。

# app/config/security.yml 
firewalls: 
    main: 
     ... 
     form_login: 
      ... 
      success_handler: authentication_success_handler 
      failure_handler: authentication_failure_handler 

接下来,你需要注册你的服务,并添加适合您的需求(可能@router@doctrine.orm.entity_manager

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

} 

如果你想挂接到另一个FOSUserBundle控制器使用this

+0

我与工作事件侦听器现在这样做,似乎是一个编译器通过覆盖不需要的服务,我可以用它来扩展默认功能的默认侦听器。 –

+0

对不起,我不能编辑我的评论,张贴作为解决方案,我会接受。 –