PHP code example of aneek / slim-event-dispatcher

1. Go to this page and download the library: Download aneek/slim-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/ */

    

aneek / slim-event-dispatcher example snippets




use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Message\ResponseInterface as Response;
use Slim\Container;
use Slim\App;
use Slim\Event\SlimEventManager;

'event.one' => [
        // First element is the FQCN class. This element is mandatory.
        // Second element is the listener priority but this is not mandatory.
        [\FooListener::class, 100],
        [\BarListener::class]    
    ],
    'event.two' => [
        [\BazListener::class]
    ]
];

$container = new Container();
$container['event_manager'] = function ($c) use($events) {
    $emitter = new SlimEventManager($events);
    return $emitter;
};

$app = new App($container);

$app->get('/hello/{name}', function (Request $request, Response $response, $args) {
    $this->get('event_manager')->emit('event.one', 'parameter_one', 'parameter_two');
    return $response->write("Hello, " . $args['name']);
});

$app->run();



use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Message\ResponseInterface as Response;
use Slim\Container;
use Slim\App;
use Slim\Event\SlimEventManager;

manager to dependency.
$container['event_manager'] = function ($c) {
    $emitter = new SlimEventManager();
    return $emitter;
};

$app = new App($container);

$app->getContainer()->get('event_manager')->add('event.one', function ($event, $param_1, $param_2) {
    // Do processing of the event.
    echo $param_1; // Prints parameter_one
    echo $param_2; // Prints parameter_two
});


$app->get('/hello/{name}', function (Request $request, Response $response, $args) {
    $this->get('event_manager')->emit('event.one', 'parameter_one', 'parameter_two');
    return $response->write("Hello, " . $args['name']);
});

$app->run();