PHP code example of spiral-packages / league-event

1. Go to this page and download the library: Download spiral-packages/league-event 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/ */

    

spiral-packages / league-event example snippets


protected const LOAD = [
    // ...
    \Spiral\League\Event\Bootloader\EventBootloader::class,
];

namespace Spiral\Router\Event;

use Spiral\Router\RouteInterface;

final class RouteFound
{
    public function __construct(
        public readonly RouteInterface $route
    ) {
    }
}

$this->container->get(EventDispatcherInterface::class)->dispatch(new RouteNotFound($request));

namespace App\Listener;

use Spiral\Events\Attribute\Listener;
use Spiral\Router\Event\RouteFound;

class RouteListener
{
    public function __invoke(RouteFound $event): void
    {
        // ...
    }
}


// file app/config/events.php

use App\Listener\RouteListener;
use Spiral\Events\Config\EventListener;
use Spiral\Router\Event\RouteFound;

return [
    'listeners' => [
        // without any options
        RouteFound::class => [
            RouteListener::class,
        ],

        // OR

        // with additional options
        RouteFound::class => [
            new EventListener(
                listener: RouteListener::class,
                method: 'onRouteFound',
                priority: 1
            ),
        ],
    ]
];

namespace App\Listener;

use Spiral\Events\Attribute\Listener;
use Spiral\Router\Event\RouteFound;

#[Listener]
class RouteListener
{
    public function __invoke(RouteFound $event): void
    {
        // ...
    }
}

namespace App\Listener;

use Spiral\Events\Attribute\Listener;
use Spiral\Router\Event\RouteFound;

#[Listener(event: RouteFound::class, method: 'onRouteFound', priority: 1)]
class RouteListener
{
    public function onRouteFound(RouteFound $event): void
    {
        // ...
    }
}

namespace App\Listener;

use Spiral\Events\Attribute\Listener;
use Spiral\Router\Event\RouteFound;

class RouteListener
{
    #[Listener(priority: 1)]
    public function onRouteFound(RouteFound $event): void
    {
        // ...
    }
}