Search code examples
symfonysymfony-formssymfony4symfony-2.1

Notice: Undefined variable: eventDispatcher?


Hey brothers I'm stack in this problem I'm trying to make and event to send email to any new user done his registration but I don't know what's wrong

enter image description hereenter image description here


Solution

  • You generally do not want to create a new event dispatcher instance in your controller. When doing so your controller would also be responsible for attaching all the listeners which quite contradicts the loose coupling between the code dispatching an event and the parts of your application listening to these events.

    Instead you will probably want to use the event_dispatcher service. In modern Symfony applications all you need to do is to add an extra argument to your controller action that is type-hinted with the EventDispatcherInterface (just like you are already doing it with the password encoder):

    use Symfony\Component\EventDispatcher\EventDispatcherInterface;
    
    public function registerAction(Request $request, UserPasswordEncoderInterface $passwordEncoder, EventDispatcherInterface $eventDispatcher)
    {
        // ...
    }