简体   繁体   中英

how can I use shopware event-subscriber properly

I've created a custom plugin SwagStartup in Shopware, and it works by calling an url like "shopware.project/myDemo".

Then I tried to define/use event subscriber in this plugin, following is the code:

<?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!');
    }
}

And the XML file: 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>

When I called the url like before, I expected to see the output from the die() function, cause I thought the event Enlight_Controller_Action_PreDispatch should have been caught by the subscriber and the funciton onPreDispatch() should have been called - but it was not the case.

What's wrong? Can someone tell me?

I tried the same in a testplugin and it works fine: https://github.com/mnaczenski/SubscriberTest

It seems that your subscriber is not recognised. Which shopware version do you use?

Late reply but issue still valid with SW 6.4

What I tried to do (And worked) was

  1. remove the class from the subscriber service. Just leave the id
  2. moved the <tag above the arguments

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM