Symfony 4.4 Event Dispatcher

1.8k views Asked by At

I'm getting the following error:

Service "event_dispatcher" not found: even though it exists in the app's container, the container inside "App\Controller\RatingApiController" is a smaller service locator that only knows about the "form.factory", "http_kernel", "parameter_bag", "request_stack", "router", "security.authorization_checker", "security.csrf.token_manager", "security.token_storage", "serializer", "session" and "twig" services. Try using dependency injection instead.

I'm used to using

 $this->get('event_dispatcher')->dispatch( AverageRatingEvent::NAME, new AverageRatingEvent($rating));

for dispatching an event but 'event_dipatcher' is giving an error I can't seem to find the source.

This is part of my controller:

<?php

namespace App\Controller;

use Symfony\Component\Routing\Annotation\Route;
use Doctrine\ODM\MongoDB\MongoDBException;
use Symfony\Component\HttpFoundation\RedirectResponse;
use App\Document\Rating;
use App\Event\AverageRatingEvent;
use App\Form\Type\RatingType;
use App\Helpers\FormErrorsSerializer;
use Doctrine\ODM\MongoDB\DocumentManager;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\EventDispatcher\Event;

class RatingApiController extends AbstractController
{
    private $documentManager;
    public function __construct(DocumentManager $documentManager)
    {
        $this->documentManager = $documentManager;
    }

class RatingApiController extends AbstractController
{
    private $documentManager;
    public function __construct(DocumentManager $documentManager)
    {
        $this->documentManager = $documentManager;
    }

        
    /**
     * @Route("api/rating/create", name="CreateRating",  methods={"POST"})
     * @param DocumentManager $dm
     * @param Request $request
     * @return RedirectResponse|Response
     * @throws MongoDBException
     */
    public function addRating(Request $request, EventDispatcherInterface $dispatcher)
    {
        $response = [];

        $form = $this->
        createForm(RatingType::class, new Rating() ,array('csrf_protection' => false));  
        

        $request = json_decode($request->getContent(), true);
        
        $form->submit($request);

        if($form->isSubmitted() && $form->isValid())
        {
        
           $rating = $form->getData();

           $this->documentManager->persist($rating);
           $this->documentManager->flush();
           
        //    $event = new AverageRatingEvent($rating);
           $this->get('event_dispatcher')->dispatch( AverageRatingEvent::NAME, new AverageRatingEvent($rating));
        //    $dispatcher->dispatch(AverageRatingEvent::NAME, $event);

}
}
1

There are 1 answers

0
Maatoug Omar On BEST ANSWER

the event dispatcher is not contained in AbstractController container.

check the symfony documentation: here u can find a example how to use the event dispatcher: https://symfony.com/doc/4.4/components/event_dispatcher.html