Symfony - Event not being dispatched - php

This is the first time ever I am working with creating custom event dispatcher and subscriber so I am trying to wrap my head around it and I cant seem to find out why my custom event is not being dispatched.
I am following the documentation and in my case I need to dispatch an event as soon as someone registers on the site.
so inside my registerAction() I am trying to dispatch an event like this
$dispatcher = new EventDispatcher();
$event = new RegistrationEvent($user);
$dispatcher->dispatch(RegistrationEvent::NAME, $event);
This is my RegistrationEvent class
namespace AppBundle\Event;
use AppBundle\Entity\User;
use Symfony\Component\EventDispatcher\Event;
class RegistrationEvent extends Event
{
const NAME = 'registration.complete';
protected $user;
public function __construct(User $user)
{
$this->user = $user;
}
public function getUser(){
return $this->user;
}
}
This is my RegistrationSubscriber class
namespace AppBundle\Event;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
use Symfony\Component\HttpKernel\KernelEvents;
class RegistrationSubscriber implements EventSubscriberInterface
{
public static function getSubscribedEvents()
{
return array(
KernelEvents::RESPONSE => array(
array('onKernelResponsePre', 10),
array('onKernelResponsePost', -10),
),
RegistrationEvent::NAME => 'onRegistration'
);
}
public function onKernelResponsePre(FilterResponseEvent $event)
{
// ...
}
public function onKernelResponsePost(FilterResponseEvent $event)
{
// ...
}
public function onRegistration(RegistrationEvent $event){
var_dump($event);
die;
}
}
After doing this, I was hoping that the registration process would stop at the function onRegistration but that did not happen, I then looked at the Events tab of the profiler and I do not see my Event listed their either.
What am I missing here? A push in right direction will really be appreciated.
Update:
I thought i need to register a service for the custom event so I added the following code inside services.yml
app.successfull_registration_subscriber:
class: AppBundle\Event\RegistrationSubscriber
arguments: ["#doctrine.orm.entity_manager"]
tags:
- { name: kernel.event_subscriber}
Inside the Event tab of profiler I do see my custom event being listed but it still does not dispatch.

By creating your own EventDispatcher instance you dispatch an event that can never be listened to by other listeners (they are not attached to this dispatcher instance). You need to use the event_dispatcher service to notify all listeners you have tagged with the kernel.event_listener and kernel.event_subscriber tags:
// ...
class RegistrationController extends Controller
{
public function registerAction()
{
// ...
$this->get('event_dispatcher')->dispatch(RegistrationEvent::NAME, new RegistrationEvent($user););
}
}

Duplicate of dispatcher doesn't dispatch my event symfony
With auto-wiring, it is now better to inject the EventDispatcherInterface
<?php
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
//...
class DefaultController extends Controller
{
public function display(Request $request, EventDispatcherInterface $dispatcher)
{
//Define your event
$event = new YourEvent($request);
$dispatcher->dispatch(YourEvent::EVENT_TO_DISPATCH, $event);
}
}

Related

How I can fire publish method once the event is fired?

I have the following event class definition:
use Symfony\Contracts\EventDispatcher\Event;
class CaseEvent extends Event
{
public const NAME = 'case.event';
// ...
}
And I have created a subscriber as follow:
use App\Event\CaseEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class CaseEventListener implements EventSubscriberInterface
{
public static function getSubscribedEvents(): array
{
return [CaseEvent::NAME => 'publish'];
}
public function publish(CaseEvent $event): void
{
// do something
}
}
I have also defined the following at services.yaml:
App\EventSubscriber\CaseEventListener:
tags:
- { name: kernel.event_listener, event: case.event}
Why when I dispatch such event as follow the listener method publish() is never executed?
/**
* Added here for visibility but is initialized in the class constructor
*
* #var EventDispatcherInterface
*/
private $eventDispatcher;
$this->eventDispatcher->dispatch(new CaseEvent($args));
I suspect the problem is kernel.event_listener but not sure in how to subscribe the listener to the event properly.
Change your subscriber so getSubscribedEvents() reads like this:
public static function getSubscribedEvents(): array
{
return [CaseEvent::class => 'publish'];
}
This takes advantage of changes on 4.3; where you no longer need to specify the event name, and makes for the simpler dispatching you are using (dispatching the event object by itself, and omitting the event name).
You could have also left your subscriber as it was; and change the dispatch call to the “old style”:
$this->eventDispatcher->dispatch(new CaseEvent($args), CaseEvent::NAME);
Also, remove the event_listener tags from services.yaml. Since you are implementing EventSubscriberInterface, you do not need to add any other configuration.

Symfony 3 - Can't pass token_storage to from subscriber

I want to get current logged user in form event but for some reason I can't get it to work.
I used services to inject token_storage and create constructor method to fetch token storage instance but I got error right at constructor:
Type error: Argument 1 passed to AdminBundle\Form\EventListener\CompanyFieldSubscriber::__construct() must implement interface Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface, none given
I am not sure what is the problem and how to fix it. Does someone knows where is the problem?
EDIT 1:
I think that I found out where is the problem but I can't find "good" solution. I call this event in form type in this way:
->addEventSubscriber(new CompanyFieldSubscriber());
Problem is that I am not using 'service/dependency injection' to create event and I am sending nothing to constructor. That's why I have this error (not 100% sure to be hones).
Since I have around 20-30 forms and new forms will come in time I need to create service for each form that requires user (or token_storage) instance and as a argument call token_storage or this event subscriber as a argument of service.
I know that it will work if I create each form as a service and pass required data as arguments but is there way to process this "automatically" without creating new service for every form that needs to have some user data interaction in form events?
EDIT 2:
As suggested I tried to change event subscriber constructor but I got same error with different class name.
New code:
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage;
public function __construct(TokenStorage $tokenStorage)
{
$this->tokenStorage = $tokenStorage;
}
New error:
Type error: Argument 1 passed to AdminBundle\Form\EventListener\CompanyFieldSubscriber::__construct() must be an instance of Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage, none given
This is code I am using:
Services:
admin.form.event_listener.company:
class: AdminBundle\Form\EventListener\CompanyFieldSubscriber
arguments: ['#security.token_storage']
tags:
- { name: form.event_listener }
Event Listener:
namespace AdminBundle\Form\EventListener;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
class CompanyFieldSubscriber implements EventSubscriberInterface
{
private $tokenStorage;
private $user;
public function __construct(TokenStorageInterface $tokenStorage)
{
$this->tokenStorage = $tokenStorage;
$this->user = $this->tokenStorage->getToken()->getUser();
}
public static function getSubscribedEvents()
{
return [
FormEvents::PRE_SET_DATA => 'preSetData',
FormEvents::PRE_SUBMIT => 'preSubmitData',
];
}
public function preSetData(FormEvent $event)
{
$form = $event->getForm();
if (in_array("ROLE_SUPER_ADMIN", $this->user->getRoles())) {
$form->add('company', EntityType::class, [
'class' => 'AppBundle:Company',
'choice_label' => 'name'
]);
}
}
public function preSubmitData(FormEvent $event)
{
$form = $event->getForm();
$bus = $form->getData();
if (!in_array("ROLE_SUPER_ADMIN", $this->user->getRoles())) {
$bus->setCompany($this->user->getCompany());
}
}
}
You call subscriber in wrong way when you use:
new CompanyFieldSubscriber()
You do not pass TokenStorageInterface to subscriber constructor. Call it as a service, if it is in controller then:
->addEventSubscriber($this->get('admin.form.event_listener.company'));
if it is in form than pass from controller to form
$this->get('admin.form.event_listener.company')
as option and then use it in form
for Symfony >= 4
use Symfony\Component\Security\Core\Security;
class ExampleService
{
private $security;
public function __construct(Security $security)
{
$this->security = $security;
}
public function someMethod()
{
$user = $this->security->getUser();
}
}
See doc: https://symfony.com/doc/current/security.html#fetching-the-user-from-a-service

Async Symfony events

I'm trying to use https://github.com/fervo/FervoDeferredEventBundle in order to have async events to store data in my DB. The event is added to the RabbitMQ Queue correctly but the event listener and the event itself is never executed.
I'm trying in both ways: first letting the listener to do the job and also dispatching manually the deferevent.
Any idea?
Thank you!
My code:
in my Controller:
$event = new DeferEvent('save.data', new SaveDataEvent($data));
$this->get('event_dispatcher')->dispatch('fervo.defer', $event);
SaveDataEvent.php
<?php
namespace AppBundle\Event;
use Symfony\Component\EventDispatcher\Event;
class SaveDataEvent extends Event
{
protected $data;
public function __construct($data)
{
$this->data = $data;
}
public function getData()
{
return $this->data;
}
public function saveData()
{
$data = $this->getData();
// do more stuff
}
}
SaveDataListener.php
<?php
namespace AppBundle\EventListener;
use AppBundle\Event\SaveDataEvent;
class SaveDataListener
{
/**
* #var SaveDataEvent
*/
public function onSendData(SaveDataEvent $event)
{
$data = $event->saveData();
}
}
services.yml
app.save_data_listener:
class: AppBundle\EventListener\SaveDataListener
tags:
-
name: kernel.event_listener #fervo_deferred_event.listener
event: send.data
Reading the docs, you should fix the services.yml:
app.save_data_listener:
class: AppBundle\EventListener\SaveDataListener
tags:
name: fervo_deferred_event.listener
event: fervo.defer
Replace fervo.defer with the first value passed to dispatch() method if you want, it's the name of the dispatched event.

How to inject controller into EventDispatcher in Symfony2

I want to do some post-processing after sending a response object in my Symfony controller. Problem is, the post-processing requires other methods contained in my controller object. I'd like to do something like this:
public function testAction() {
$dispatcher = new EventDispatcher();
$dispatcher->addListener('kernel.terminate', function (Event $event) {
$controller->get('logger');
$logger->info('hello');
});
return new Response();
}
How can I inject the $controller variable into my kernel.terminate post-processing?
it seems you need only the container in your service. To get the container injected into your event listener I prefer to create a separate EventListener which you have to register in your container see code:
First create event listener class:
<?php
namespace Acme\DemoBundle\Listener;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\DependencyInjection\ContainerInterface;
class RequestListener
{
protected $container;
public function __construct(ContainerInterface $container)
{
$this->container = $container;
}
public function onKernelRequest(GetResponseEvent $event)
{
$request = $event->getRequest();
$logger = $this->container->get('logger')->getToken();
$logger->info('.....');
}
}
As you can see, we have now the service container injected and we are able to use it.
Next you have to register the service and inject the service container:
services:
acme.demo.listener.request:
class: Acme\DemoBundle\Listener\RequestListener
arguments: [ #service_container ]
tags:
- { name: kernel.event_listener, event: kernel.request, method: onKernelRequest }
Notice in your case you have to select the event you wanna inject to. In my case I used the kernel.request event. You have to select the kernel.terminate event.
That can also be helpful: http://symfony.com/doc/current/cookbook/service_container/event_listener.html

Why is symfony2 not calling my event listeners?

I have a program with two bundles. One of them (CommonBundle) dispatches an event "common.add_channel", while a service on the other one (FetcherBundle) was supposed to be listening to it. On the profiler, I can see the event common.add_channel in the "Not Called Listeners" section. I don't get why symfony is not registering my listener.
This is my action, inside CommonBundle\Controller\ChannelController::createAction:
$dispatcher = new EventDispatcher();
$event = new AddChannelEvent($entity);
$dispatcher->dispatch("common.add_channel", $event);
This is my AddChannelEvent:
<?php
namespace Naroga\Reader\CommonBundle\Event;
use Symfony\Component\EventDispatcher\Event;
use Naroga\Reader\CommonBundle\Entity\Channel;
class AddChannelEvent extends Event {
protected $_channel;
public function __construct(Channel $channel) {
$this->_channel = $channel;
}
public function getChannel() {
return $this->_channel;
}
}
This was supposed to be my listener (FetcherService.php):
<?php
namespace Naroga\Reader\FetcherBundle\Service;
class FetcherService {
public function onAddChannel(AddChannelEvent $event) {
die("It's here!");
}
}
And here's where I register my listener (services.yml):
kernel.listener.add_channel:
class: Naroga\Reader\FetcherBundle\Service\FetcherService
tags:
- { name: kernel.event_listener, event: common.add_channel, method: onAddChannel }
What am I doing wrong? Why isn't symfony calling the event listener when I dispatch common.add_channel?
The new event dispatcher doesn't know anything about the listeners set on another dispatcher.
In your controller, you need to access the event_dispatcher service. A Compiler Pass of the Framework Bundle attached all listeners to this dispatcher. To get the service, use the Controller#get() shortcut:
// ...
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
class ChannelController extends Controller
{
public function createAction()
{
$dispatcher = $this->get('event_dispatcher');
// ...
}
}

Categories