2017-08-09 1 views
1

LastLoginListenerをオーバーライドして機能を追加しようとしています。Fosuserbundleはイベントリスナーをオーバーライドします

私は、here を説明したように、リスナー自身があるservices.yml

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

<?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); 
    } 

AppBundle \依存性の注入\ OverrideServiceCompilerPass.phpで

を思わそれをやろうとしているメートルバンドルからコピーされます。

"/vendor/composer/../../src/AppBundle/DependencyInjection/OverrideServiceCompilerPass.php"ファイルでオートローダが期待するクラス "AppBundle \ DependencyInjection \ OverrideServiceCompilerPass"を定義します。ファイルが見つかりましたが、クラスがそれに含まれていなかった、クラス名または名前空間はおそらくタイプミスを持っています。 DebugClassLoader.phpで (ライン261)

私の目標は、リスナーとの最後のログインのIPアドレスを追加することですが、私は私がしようとしている役割と登録日 を追加するために別のものを作成する必要があります何かをハッキングするのではなく、正しい方法で行うこと

答えて

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

申し訳ありませんが、私のコメントを編集できません。それを解決策として投稿してください。 –

関連する問題