1

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?

nweicp
  • 107
  • 1
  • 7

2 Answers2

0

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?

mnaczenski
  • 1,508
  • 7
  • 12
  • Thanks for your effor! My experience is a little bit confusing. The code that I posted did really NOT work. But now it WORKS. I don't know why! I did NOT change anything... – nweicp Sep 03 '18 at 11:25
  • Maybe you did not clear the cache before? – Alex Dec 18 '21 at 18:45
0

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
EigenFool
  • 443
  • 1
  • 6
  • 14