2017-08-09 71 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

對不起,我不能編輯我的評論,張貼作爲解決方案,我會接受。 –