簡體   English   中英

當商店用戶在Sylius進行電子郵件確認時,如何收聽事件?

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

在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的任何條目將始終被忽略。

可能的解決方案:

簡而言之,我嘗試了幾種方法,並在考慮其他方法(甚至是那些非常丑陋的方法):

  1. 聽適當的事件->沒有機會
  2. 覆蓋UserController->沒有機會或做錯了方法
  3. 創建一個cronjob,它將在5分鍾左右的間隔內獲取sylius_customer-table,以查找最近啟用了其帳戶的客戶->是的,可以做到,但也很丑陋

還有嗎

任何想法如何解決我們的問題?

嘗試以下代碼,並在打開驗證鏈接后成功接收到轉儲消息(在我的情況下為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');
    }

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM