0

This is my Action

public function indexAction(Request $request,EventDispatcherInterface $dispatcher){
$dispatcher->dispatch(TestEvent::Name, new TestEvent(new test());
...
}

but it throws the error ...MyController::indexAction()" requires that you provide a value for the "$dispatcher" argument.

I'm on an old symfony 3.4 project and every single doc page I've read says that the EventDispatcherInterface can be used this way with no config needed.

no0ob
  • 335
  • 4
  • 18
  • Show your services.yml and the autowire config. Is this controller tagged with `controller.service_arguments` ? – blahy Jul 29 '20 at 20:29
  • @blahy Sorry I'm answering late, I've asked the old guy of the project he said it was a `2.8` Symfony updated to `3.4` which maybe some parts are still not truly `3.4`. btw I can't find the `controller.service_arguments` in services.yml and not in any other files. – no0ob Jul 30 '20 at 21:08
  • see this https://symfony.com/doc/3.4/controller.html#fetching-services-as-controller-arguments . It was added in 3.3 and uses autowiring https://symfony.com/doc/3.4/service_container/autowiring.html but if you dont have this setup then it wont work. In symfony2 you accessed services from service container via $this->get('service_alias'), where alias is from services.yml ('dispatcher' or 'event_dispatcher' - check with `php app/console debug:container`). This is for 2.8 https://symfony.com/doc/2.8/controller.html#accessing-other-services . You have a nice hybrid there. – blahy Jul 30 '20 at 21:19
  • and see this services.yml in symfony3.3 https://github.com/symfony/symfony-standard/blob/3.3/app/config/services.yml#L8 and https://github.com/symfony/symfony-standard/blob/3.3/app/config/services.yml#L27 . If this is configured correctly in your project (so autowire and controller's actions can accept services as parameters) your way of doing this should work – blahy Jul 30 '20 at 21:31
  • `event_dispatcher` works, It's just deprecated maybe because IDE thinks I'm really on a `3.4` but I see it as the fastest option rn. I really want to avoid touching any config file for now. – no0ob Jul 30 '20 at 22:26

0 Answers0