Ho un programma con due pacchetti. Uno di essi (CommonBundle) invia un evento "common.add_channel", mentre un servizio sull'altro (FetcherBundle) doveva ascoltarlo. Nel profiler, posso vedere l'evento common.add_channel nella sezione "Not Called Listeners". Non capisco perché symfony non stia registrando il mio ascoltatore.Perché symfony2 non chiama i miei ascoltatori di eventi?
Questa è la mia azione, all'interno CommonBundle\Controller\ChannelController::createAction
:
$dispatcher = new EventDispatcher();
$event = new AddChannelEvent($entity);
$dispatcher->dispatch("common.add_channel", $event);
Questo è il mio 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;
}
}
Questo doveva essere il mio ascoltatore (FetcherService.php):
<?php
namespace Naroga\Reader\FetcherBundle\Service;
class FetcherService {
public function onAddChannel(AddChannelEvent $event) {
die("It's here!");
}
}
E qui è dove registro il mio listener (services.yml):
kernel.listener.add_channel:
class: Naroga\Reader\FetcherBundle\Service\FetcherService
tags:
- { name: kernel.event_listener, event: common.add_channel, method: onAddChannel }
Cosa sto sbagliando? Perché symfony non chiama il listener di eventi quando invio common.add_channel?
standard domanda symfony: avete svuotato la cache dopo l'aggiunta di ascoltatore in services.yml? –
Sì. Non è necessario, eventuali modifiche a services.yml vengono ricompilate nella cache se si utilizza l'app_dev.php in ogni pagehit. L'ho chiarito molte volte, però. –