Suche…


Syntax

  • $ dispatcher-> dispatch (String $ eventName, Event $ event);
  • $ dispatcher-> addListener (String $ eventName, aufrufbarer $ Listener, int $ priority = 0);
  • $ dispatcher-> addSubscriber (EventSubscriberInterface $ Abonnent);

Bemerkungen

  • Es ist oft am besten, eine einzelne Instanz von EventDispatcher in Ihrer Anwendung zu verwenden, die Sie in die Objekte einfügen, die Ereignisse auslösen müssen.
  • Es empfiehlt sich, einen einzigen Standort zu haben, an dem Sie die Konfiguration Ihres EventDispatcher verwalten und Ereignis-Listener hinzufügen. Das Symfony-Framework verwendet den Dependency Injection Container.
  • Mithilfe dieser Muster können Sie Ihre Ereignis-Listener problemlos ändern, ohne den Code eines Moduls ändern zu müssen, das Ereignisse auslöst.
  • Die Entkopplung des Ereignisversands von der Konfiguration des Ereignislisteners macht den Symfony EventDispatcher so leistungsfähig
  • Der EventDispatcher hilft Ihnen, das Open / Closed-Prinzip zu erfüllen.

Event Dispatcher - Schnellstart

use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Component\EventDispatcher\Event;
use Symfony\Component\EventDispatcher\GenericEvent;

// you may store this in a dependency injection container for use as a service
$dispatcher = new EventDispatcher();

// you can attach listeners to specific events directly with any callable
$dispatcher->addListener('an.event.occurred', function(Event $event) {
    // process $event
});

// somewhere in your system, an event happens
$data = // some important object
$event = new GenericEvent($data, ['more' => 'event information']);

// dispatch the event
// our listener on "an.event.occurred" above will be called with $event
// we could attach many more listeners to this event, and they too would be called
$dispatcher->dispatch('an.event.occurred', $event);

Event-Abonnenten

use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\EventDispatcher\Event;

$dispatcher = new EventDispatcher();

// you can attach event subscribers, which allow a single object to subscribe 
// to many events at once
$dispatcher->addSubscriber(new class implements EventSubscriberInterface {
    public static function getSubscribedEvents()
    {
        // here we subscribe our class methods to listen to various events
        return [
            // when anything fires a "an.event.occurred" call "onEventOccurred"
            'an.event.occurred' => 'onEventOccurred',
            // an array of listeners subscribes multiple methods to one event
            'another.event.happened' => ['whenAnotherHappened', 'sendEmail'],
        ];
    }

    function onEventOccurred(Event $event) {
        // process $event
    }

    function whenAnotherHappened(Event $event) {
        // process $event
    }

    function sendEmail(Event $event) {
        // process $event
    }
});


Modified text is an extract of the original Stack Overflow Documentation
Lizenziert unter CC BY-SA 3.0
Nicht angeschlossen an Stack Overflow