代码之家  ›  专栏  ›  技术社区  ›  itinance

当商店用户在Sylius进行电子邮件确认时,如何收听事件?

  •  0
  • itinance  · 技术社区  · 8 年前

    在用户点击这个事件之后,由于在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\u验证.token”,它只负责创建最终的身份验证令牌。

    然后我翻阅了西尔尤斯的密码,找到了 用户控制器 以及行动 ,听起来很有希望。

    在此操作中,将调度多个事件:

    $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';
    

    调试:事件调度器

    我们已经有了一个定制的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,覆盖原始控制器并复制 验证操作 -方法(尽管这对于长尾来说不是一个好的选择)

    还是没有成功。

    虽然我们成功地重写了几个控制器,但UserController似乎位于一个无法更改为其他类的孤岛上:

    我们的 看起来像这样:

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

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

    可能的解决方案:

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

    1. 覆盖UserController->没有机会,或者使用了错误的方法

    你知道怎么解决我们的问题吗?

    1 回复  |  直到 6 年前
        1
  •  2
  •   Dr.X    8 年前

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