PHP code example of symplify / symfony-event-dispatcher

1. Go to this page and download the library: Download symplify/symfony-event-dispatcher library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

symplify / symfony-event-dispatcher example snippets


// app/EventSubscriber/CheckRequestEventSubscriber.php

namespace App\EventSubscriber;

use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\HttpKernel\Event\GetResponseEvent

final class CheckRequestEventSubscriber implements EventSubscriberInterface
{
    /**
     * @var bool
     */
    public $isUserNotified = false;

    public static function getSubscribedEvents(): array
    {
        // in format ['event name' => 'public function name that will be called']
        return [KernelEvents::REQUEST => 'validateRequest'];
    }


    // Appropriate event object is passed in arguments
    public function validateRequest(GetResponseEvent $event): void
    {
        // some logic to send notification
        $this->isUserNotified = true;
    }
}