【问题标题】:how can I use shopware event-subscriber properly如何正确使用 shopware event-subscriber
【发布时间】:2019-02-08 09:01:24
【问题描述】:

我在 Shopware 中创建了一个自定义插件 SwagStartup,它通过调用诸如“shopware.project/myDemo”之类的 url 来工作。

然后我尝试在这个插件中定义/使用事件订阅者,代码如下:

<?php
namespace SwagStartup\Subscriber;
use Enlight\Event\SubscriberInterface;

class RoutingSubscriber implements SubscriberInterface
{
    public static function getSubscribedEvents(){
        return ['Enlight_Controller_Action_PreDispatch' => 'onPreDispatch'];
    }
    public function onPreDispatch(\Enlight_Event_EventArgs $args){
        die('here!');
    }
}

还有 XML 文件:Resources/services.xml:

<?xml version="1.0" ?>
<container xmlns="http://symfony.com/schema/dic/services"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
    <services>
        <service id="swag_startup.subscriber.routing_subscriber"
                 class="SwagStartup\Subscriber\RoutingSubscriber">
            <tag name="shopware.event_subscriber"/>
        </service>
    </services>
</container>

当我像以前一样调用 url 时,我希望看到 die() 函数的输出,因为我认为事件 Enlight_Controller_Action_PreDispatch 应该已被订阅者和函数 捕获onPreDispatch() 应该被调用 - 但事实并非如此。

怎么了?谁能告诉我?

【问题讨论】:

  • 乍一看还不错。你清除缓存了吗?插件是否已安装并处于活动状态?

标签: plugins shopware subscriber


【解决方案1】:

延迟回复但问题在 SW 6.4 中仍然有效

我试图做(并且工作)是

  1. 从订户服务中删除该类。留下id就行了

【讨论】:

    【解决方案2】:

    我在 testplugin 中尝试了同样的方法,它工作正常:https://github.com/mnaczenski/SubscriberTest

    您的订阅者似乎未被识别。您使用哪个版本的商店软件?

    【讨论】:

    • 感谢您的努力!我的经历有点混乱。我发布的代码确实不起作用。但现在它可以工作了。我不知道为什么!我没有改变任何东西......
    • 可能你之前没有清除缓存?
    猜你喜欢
    • 2021-01-11
    • 2023-01-18
    • 2020-04-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-29
    • 2022-09-26
    • 1970-01-01
    相关资源
    最近更新 更多