如何覆盖FOSUserBundle的EmailConfirmationListener [英] How to override FOSUserBundle's EmailConfirmationListener

查看:125
本文介绍了如何覆盖FOSUserBundle的EmailConfirmationListener的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我激活了FOSUserBundle的用户确认。但是我不想收到原始听众的回复

I activated user confirmation for FOSUserBundle. But I don't want to take the response from the original listener

$url = $this->router->generate('fos_user_registration_check_email');
$event->setResponse(new RedirectResponse($url));

我想选择另一条路线。我试图扩展EventListener

I want to chose another route. I tried to extend the EventListener

namespace Acme\MyBundle\EventListener;

use FOS\UserBundle\EventListener\EmailConfirmationListener as BaseListener;
// ...

class EmailConfirmationListener extends BaseListener
{
    public function onRegistrationSuccess(FormEvent $event)
    {    
        $url = $this->router->generate('fos_user_registration_check_email');
        $event->setResponse(new RedirectResponse($url));
    }
}

不幸的是,EventListener似乎不可扩展,就像控制器或表格一样。 (以防万一你想知道:当然我的捆绑包是FOSUserBundle的孩子。)

Unfortunately, EventListeners don't seem to be extendable, just as Controllers or Forms are. (Just in case you wonder: of course my bundle is a child of the FOSUserBundle.)

所以我想避免在供应商文件夹中直接编辑这两行这样做是非常糟糕的做法!)。那么我出了这个灾难的方法是什么?

So I want to avoid editing those two lines directly in the vendor folder (as it would be very bad practice to do so!). So what are my ways out of this calamity?

推荐答案

只需覆盖服务 fos_user.listener.email_confirmation 通过在您的 config.yml 中创建一个名称相同的服务...

Just override the service fos_user.listener.email_confirmation by creating a service with the same name in your config.yml ...

# app/config/config.yml

services:
    fos_user.listener.email_confirmation:
        class:        "Acme\MyBundle\EventListener\EmailConfirmationListener"
        arguments:    ["@fos_user.mailer", "@fos_user.util.token_generator", "@router", "@session"]
        tags:
            - { name: kernel.event_subscriber }

...甚至更清洁 - 创建正在使用的参数您的服务:

... or even cleaner - create a parameter that's being used by your service:

parameters:
    my.funky_parameter.class: "Acme\MyBundle\EventListener\EmailConfirmationListener"

services:
    fos_user.listener.email_confirmation:
        class: "%my.funky_parameter.class%"
        # ...

...或者你的包的xml / yml / php配置文件由bundle的扩展名加载。在选择这种方式时,请确保您的软件包在FOSUserBundle之后在 AppKernel.php 中注册。

... or inside your bundle's xml/yml/php configuration file loaded by the bundle's extension. Make sure your bundle is being registered after FOSUserBundle in AppKernel.php when choosing this way.

...或最好的方法:

... or the best method:

在编译过程中更改原始服务的类名作为文档章节 如何覆盖捆绑的任何部分 建议。

change the original service's class name in a compiler pass as the documentation chapter How to Override any Part of a Bundle suggests.

也许潜入如何在选择此选项之前,请使用编译器通行证

这篇关于如何覆盖FOSUserBundle的EmailConfirmationListener的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆