首页 文章

当商店用户在Sylius做电子邮件确认时,如何听取此事件?

提问于
浏览
0

在Sylius中注册商店用户作为我的Web应用程序的基础框架后,用户将收到带有验证链接的电子邮件 .

在用户单击此事件后,由于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似乎位于一个无法更改为另一个类的岛上:

我们的 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的任何条目将始终被忽略 .

可能的解决方案:

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

  • 听取适当的事件 - >没有机会

  • 覆盖UserController - >没有机会或做错了

  • 创建一个cronjob,它将以5分钟左右的间隔获取sylius_customer-table,以找到最近启用了他们帐户的客户 - >是的,可以完成但是也很难看

别的什么?

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

1 回答

  • 1

    尝试下面的代码,并在打开验证链接后成功收到转储消息(在我的情况下是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');
        }
    
    }
    

相关问题