店铺用户在Sylius进行邮件确认时如何监听事件?

How to listen to the event when a shop user is doing email confirmation at Sylius?

商店用户在 Sylius 中注册为我的 Web 应用程序的基础框架后,该用户将收到一封带有验证的电子邮件 link。

用户点击此事件后,由于 Sylius 背后发生了一些神奇的事情,用户将在数据库中启用。

我想捕获此事件,以便能够在此步骤上添加更多操作。

采取的步骤:

首先,我试图找到一个事件,可以在用户身份验证成功的情况下触发。

bin/console debug:event-dispatcher

"sylius.admin_user.post_create" event
"sylius.admin_user.post_update" event
"sylius.admin_user.pre_delete" event
"sylius.admin_user.pre_update" event
"sylius.cart_change" event
"sylius.channel.pre_delete" event
"sylius.customer.post_register" event
"sylius.customer.pre_register" event
"sylius.customer.pre_update" event
"sylius.menu.shop.account" event
"sylius.oauth_user.post_create" event
"sylius.oauth_user.post_update" event
"sylius.oauth_user.pre_delete" event
"sylius.order.post_address" event
"sylius.order.post_complete" event
"sylius.order.post_payment" event
"sylius.order.post_select_shipping" event
"sylius.order.pre_complete" event
"sylius.order_item.pre_create" event
"sylius.order_item.pre_delete" event
"sylius.order_item.pre_update" event
"sylius.product.initialize_update" event
"sylius.product.pre_create" event
"sylius.product.pre_update" event
"sylius.product_review.pre_create" event
"sylius.product_variant.initialize_update" event
"sylius.shipment.post_ship" event
"sylius.shop_user.post_create" event
"sylius.shop_user.post_update" event
"sylius.shop_user.pre_delete" event
"sylius.taxon.pre_create" event
"sylius.taxon.pre_update" event
"sylius.user.email_verification.token" event
"sylius.user.password_reset.request.pin" event
"sylius.user.password_reset.request.token" event
"sylius.user.post_email_verification" event
"sylius.user.pre_password_change" event
"sylius.user.pre_password_reset" event
"sylius.user.security.impersonate" event
"sylius.user.security.implicit_login" event

这将列出许多事件,但除了“sylius.user.email_verification.token”之外,没有任何内容表明任何电子邮件验证过程,它只负责创建最终的身份验证令牌。

然后我逐步查看了 Sylius 的代码,发现 UserController 和操作 verifyAction,听起来很有希望。

在此操作中,调度了几个事件:

$eventDispatcher->dispatch(UserEvents::PRE_EMAIL_VERIFICATION, new GenericEvent($user));

$eventDispatcher->dispatch(UserEvents::POST_EMAIL_VERIFICATION, new GenericEvent($user));

这些常量定义为:

public const PRE_EMAIL_VERIFICATION = 'sylius.user.pre_email_verification';
public const POST_EMAIL_VERIFICATION = 'sylius.user.post_email_verification';

然而,这两种事件类型都没有在 symfony 的全局事件调度程序中注册,它们都没有像之前所示的 debug:event-dispatcher 列出,因此可以'不听。

我们已经有一个适用于注册过程的自定义 EventLister,但它不会捕获任何电子邮件验证事件:

/**
 * {@inheritdoc}
 */
public static function getSubscribedEvents()
{
    return [
        'sylius.customer.pre_register' => ['onCustomerPreRegister', 0],
        'sylius.customer.post_register' => ['onCustomerPostRegister', 0],
        UserEvents::POST_EMAIL_VERIFICATION => ['onPostEmailVerification', 0]
    ];
}

然后我尝试在自己的实现中覆盖 UserController,覆盖原始控制器并复制 verifyAction-Method(尽管事实上这不是一个好的选择长尾)

仍然没有成功。

虽然我们已经成功覆盖了几个控制器,但 UserController 似乎在一个无法更改为另一个的孤岛上class:

我们的services.yml看起来是这样的:

sylius.controller.shop_user:
    class: AppBundle\Controller\User\UserController
    public: true

sylius.controller.shop.homepage:
    class: AppBundle\Controller\Shop\HomepageController
    public: true

主页的自定义控制器运行良好,而 UserController 的任何条目将始终被忽略。

可能的解决方案:

简而言之,我尝试了几种方法并正在考虑其他方法(即使是那些非常难看的方法):

  1. 听取适当的事件 -> 没有机会
  2. 覆盖 UserController -> 没机会或做错了
  3. 创建一个 cronjob,它将在 5 分钟左右的时间间隔内获取 sylius_customer-table 以查找最近启用了他们帐户的客户 -> 是的,可以完成但也很丑陋

还有别的吗?

知道如何解决我们的问题吗?

尝试了下面的代码,打开验证后成功收到转储消息link(在我的例子中是http://localhost/app_dev.php/en_US/verify/mUJjxjiI0OjWhRC2

<?php

namespace AppBundle\EventListener;

use Sylius\Bundle\UserBundle\UserEvents;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\EventDispatcher\GenericEvent;

class CustomVerificationSubscriber implements EventSubscriberInterface
{
    public static function getSubscribedEvents()
    {
        return [
            UserEvents::POST_EMAIL_VERIFICATION => 'onVerification'
        ];
    }

    public function onVerification(GenericEvent $event)
    {
        dump('it works');
    }

}