Yii2 events provide a simple observer pattern implementation, allowing you to subscribe and listen for various events that occur within your application.
Install via composer
composer require laxity7/yii2-event-service
It will be an any class that contains event data. You can also use the default Yii2 event class yii\base\Event
.
For example:
namespace App\Events;
use yii\base\Event;
final readonly class PaymentEvent
{
public function __construct(
public float $amount,
public string $currency,
public string $description,
) {
}
}
It will be a class that contains a method handle
or __invoke
that will be called when the event is dispatched. The method must accept an event object as an argument.
For example:
namespace App\Events\listeners;
use App\Events\PaymentEvent;
final class PaymentListener
{
//public function __invoke(PaymentEvent $event): void
public function handle(PaymentEvent $event): void
{
Yii::info('Event: ' . get_class($event) . ' Trigger: ' . __METHOD__, __METHOD__);
}
}
Note: You can also use a closure as a listener. The closure must accept an event object as an argument.
Add the following code to your configuration file:
'components' => [
'eventDispatcher' => [
'class' => \Laxity7\Yii2\Components\EventServiceProvider::class,
'listen' => [
\App\Events\PaymentEvent::class => [
\App\Events\listeners\PaymentListener::class, // listener class
function (\App\Events\PaymentEvent $event) { // closure
Yii::info('Event: ' . get_class($event) . ' Trigger: ' . __METHOD__, __METHOD__);
},
],
],
],
],
use App\Events\PaymentEvent;
use Laxity7\Yii2\Components\Event;
$event = new PaymentEvent(100, 'USD', 'Payment for goods');
\Yii::$app->eventDispatcher->dispatch($event);
// or use the helper
Event::dispatch($event);