【问题标题】:Fosuserbundle override event listenerFosuserbundle 覆盖事件监听器
【发布时间】:2017-08-09 18:36:02
【问题描述】:

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

我正在尝试按照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 地址,但我需要创建另一个以添加角色和注册日期 我正在尝试以“正确的方式”而不是做一些骇人听闻的事情

【问题讨论】:

    标签: symfony fosuserbundle


    【解决方案1】:

    使用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

    【讨论】:

    • 我现在用一个工作的事件监听器来做这件事,似乎不需要编译器传递来覆盖服务,我可以用它来扩展默认监听器的默认功能。
    • 对不起,我无法编辑我的评论,将其作为解决方案发布,我会接受。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-04-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-17
    相关资源
    最近更新 更多