title: symfony 5 - event not being dispatched
body: <p>I use an event subscriber to handle some actions when my order form is submitted.</p>
  <p>Problem my event is not being dispached but symfony is able to find him because he tells me that my OrderEvent::ORDER_CREATE is orphan.</p>
  <p>I excpected that execution was stopped with <code>die('Hello you from subscriber');</code> but it's not.</p>
  <p>Controller</p>
  <pre class="lang-php prettyprint-override"><code>public function commanderPanierAction(Request $request, SelectionWeb $selectionWeb, TableLumineuse $tableLumineuse, EventDispatcherInterface $eventDispatcher)
  {
      // DO PREVIOUS STUFF
  
      $Order = new Order();
      $OrderForm = $this-&gt;createForm(OrderForm::class, $Order);
          
      if ($request-&gt;isMethod('POST')) {
        $OrderForm-&gt;handleRequest($request);
  
        if ($OrderForm-&gt;isSubmitted() &amp;&amp; $OrderForm-&gt;isValid()) {
            // OrderForm is valid proceed
            $eventDispatcher-&gt;dispatch(
                new OrderEvent($Order),
                OrderEvent::ORDER_CREATE
            );
        }
     } 
  </code></pre>
  <p>OrderEvent</p>
  <pre class="lang-php prettyprint-override"><code>&lt;?php
  
  namespace App\Event;
  
  use App\Entity\Order;
  use Symfony\Contracts\EventDispatcher\Event;
  
  class OrderEvent extends Event
  {
      public const ORDER_CREATE = 'order.created';
  
      protected $order;
  
      public function __construct(Order $order)
      {
          $this-&gt;order= $order;
      }
  
      public function getOrder(): Order
      {
          return $this-&gt;order;
      }
  }
  </code></pre>
  <p>OrderSubscriber</p>
  <pre class="lang-php prettyprint-override"><code>&lt;?php
  
  namespace App\EventSubscriber;
  
  use App\Event\CommandeWebEvent;
  use App\Service\SelectionWeb\SelectionWeb;
  use Doctrine\ORM\EntityManagerInterface;
  use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  
  
  class OrderSubscriber implements EventSubscriberInterface
  {
      private $entityManager;
  
      private $selectionWeb;
  
      public function __construct(EntityManagerInterface $entityManager, SelectionWeb $selectionWeb)
      {
          $this-&gt;entityManager = $entityManager;
          $this-&gt;selectionWeb = $selectionWeb;
      }
  
      public static function getSubscribedEvents()
      {
          return [
              OrderEvent::ORDER_CREATE =&gt; [
                  // The higher the number, the earlier the method is called.
                  ['processOrder', 10],
                  ['notifyOrder', -10]
              ]
          ];
      }
  
      public function processOrder(OrderEvent $event)
      {
          // TODO
          die('Hello you from subscriber');
      }
  
      public function notifyOrder(OrderEvent $event)
      {
          // TODO
      }
  }
  
  </code></pre>
  <p><strong>EDIT</strong></p>
  <p>The only workaround found (thx to @nikserg) is to inject subscriber into controller action (subscriber has dependencies) then register my subscriber as service in <code>services.yaml</code> finaly use <code>$eventDispatcher-&gt;addSubscriber($subscriber);</code> before <code>$eventDispatcher-&gt;dispatch(new OrderEvent($Order),OrderEvent::ORDER_CREATE);</code></p>
  <p>It seems all that stuff is really complex for a task as simple as that</p>
  <p><strong>EDIT2</strong></p>
  <p>I found an another way I'm able to execute my subscriber without usage of <code>$eventDispatcher-&gt;addSubscriber($subscriber);</code> and only with <code>$eventDispatcher-&gt;dispatch(new OrderEvent($Order));</code> only if I configure my subscriber as service in <code>services.yaml</code> but why symfony does need this information in services.yaml ? Thought that everything in src/ is avaible to be used as service..</p>
  <pre class="lang-yaml prettyprint-override"><code># makes classes in src/ available to be used as services
  # this creates a service per class whose id is the fully-qualified class name
  App\:
      resource: '../src/*'
      exclude: '../src/{DependencyInjection,Entity,Migrations,Tests,Kernel.php}'
  
  # If I add those It works
  App\EventSubscriber\OrderSubscriber:
      autowire: true
  </code></pre>
  <p><strong>EDIT3</strong></p>
  <p>My OrderSubscriber is loaded into container so why I should set it explicitly to being execute ? I can't figure out what's going on</p>
  <pre class="lang-sh prettyprint-override"><code>php bin/console debug:container
  ---------------------------------------- ---------------------------------------
  Service ID                               Class name
  ---------------------------------------- ----------------------------------------
  App\EventSubscriber\OrderSuscriber App\EventSubscriber\OrderSuscriber
  </code></pre>
  <p><strong>EDIT 4</strong></p>
  <p>If I set my <code>OrderSubscriber</code> explicitly there is two instances of it into container.
  Why symfony execute one set explicitly and not the one set with <code>resource: '../src/*' </code></p>