This library is an implementation of League\Event for Slim Framework

Overview

Slim Event Dispatcher

Build Status

This library is an implementation of League\Event for Slim Framework. This works with the latest version of Slim (V3).

Installation

It's recommended that you use Composer to install Slim Event Dispatcher.

$ composer require aneek/slim-event-dispatcher "^1.0"

This will install Slim Event Dispatcher and all required dependencies. This package requires PHP 5.6 or newer. Though version 1.x is not recommended for PHP 7.0 and above. The next major version (v2.x) will support PHP 7.x.

Usage

This is not a Slim Middleware but a package which integrates with your Slim application by extending the SlimEventManager class and adding it to Slim's dependency container. Currently there are two ways of integration.

Using Class Constructor

Slim\Event\SlimEventManager class accepts an array argument in it's constructor and initializes all Listeners (more information on listeners).

The below file content serves as a application loader file for Slim Framework:

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;

require 'vendor/autoload.php';

$settings = [
    'settings' => [
        'determineRouteBeforeAppMiddleware' => true,
        'displayErrorDetails' => true,
    ],
];

// Array of event Listeners
$events = [
    '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();

Using add Method with Callable

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;

require 'vendor/autoload.php';

$settings = [
    'settings' => [
        'determineRouteBeforeAppMiddleware' => true,
        'displayErrorDetails' => true,
    ],
];


$container = new Container();
// Add Event 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();

Creating Events or Listeners

Events or Listeners can be created by following the below guides from League\Event.

Issues

Please open a new issue and I will be happy to look into it.

You might also like...
Diff implementation

sebastian/diff Diff implementation for PHP, factored out of PHPUnit into a stand-alone component. Installation You can add this library as a local, pe

Mutex lock implementation

Yii Mutex This package provides mutex implementation and allows mutual execution of concurrent processes in order to prevent "race conditions". This i

A minimalistic implementation of Promises for PHP

libPromise A minimalistic implementation of Promises for PHP. Installation via DEVirion Install the DEVirion plugin and start your server. This will c

PHP's Promse implementation depends on the Swoole module.

php-promise-swoole PHP's Promse implementation depends on the Swoole module. Promise::allsettled([ /** Timer 调用 */ /** Timer call */

Pheature flags toggle CRUD implementation

Pheature Flags Toggle CRUD Pheature flags toggle CRUD implementation Installation Describe package installation composer require pheature/toggle-crud

A circular buffer implementation in PHP

Circular Buffer Installation 💡 This is a great place for showing how to install the package, see below: Run $ composer require lctrs/circular-buffer

Frequently asked questions crud implementation for laravel projects

Laravel FAQs This is a simple package to help manage frequently asked questions in a project. Installation You can install the package via composer by

A simple laravel package for wallet implementation

wallet A simple laravel package for wallet implementation. This package can basically be plugged into a laravel project and it will handle wallet impl

My own implementation of the backend challenge.
My own implementation of the backend challenge.

Millions backend challenge My own implementation of the backend challenge. Implemented features Show posts paginated, and ordered by creation date wit

Comments
  • How to emmit event on Model?

    How to emmit event on Model?

    I am using Eloquent as ORM for my model, how do I emmit event from within the models?

    Let's use this dummy class as example:

    class User
    {
        public function paySomething()
        {
            $emmiter->emmit(new SomethingPaid()); // How do you suggest to use something like this
        }
    }
    
    opened by dbrw 0
Releases(1.0)
  • 1.0(Sep 27, 2017)

    First release with PHP 5.6 and higher support. It's not recommended to use this for PHP 7.0 and above (thought it's supported). The next major release (v2.0) will be fully compatible with PHP 7.x.

    Source code(tar.gz)
    Source code(zip)
Owner
Aneek Mukhopadhyay
Aneek Mukhopadhyay
Simple Blog written with Slim Framework

SlimBlog Demo Demo Dependencies Slim Framework Eloquent Twig Bootstrap Install Run 'php composer.phar install' in root directory Run every *.sql files

Fabio Di Sotto 102 Sep 14, 2022
Implementation of an API application using the Phalcon Framework

phalcon-api Sample API using Phalcon Implementation of an API application using the Phalcon Framework https://phalcon.io Installation Clone the projec

The Phalcon PHP Framework 81 Dec 15, 2022
This repository demonstrates exemplary implementation of chat using HTTP and Websocket servers in PHP using Kraken Framework components.

This repository demonstrates exemplary implementation of chat using HTTP and Websocket servers in PHP using Kraken Framework components.

Kraken 48 Aug 11, 2021
Ecotone Framework is Service Bus Implementation. It enables message driven architecture and DDD, CQRS, Event Sourcing PHP

This is Read Only Repository To contribute make use of Ecotone-Dev repository. Ecotone is Service Bus Implementation, which enables message driven arc

EcotoneFramework 308 Dec 29, 2022
A comprehensive library for generating differences between two strings in multiple formats (unified, side by side HTML etc). Based on the difflib implementation in Python

PHP Diff Class Introduction A comprehensive library for generating differences between two hashable objects (strings or arrays). Generated differences

Chris Boulton 708 Dec 25, 2022
PHP implementation of circuit breaker pattern.

What is php-circuit-breaker A component helping you gracefully handle outages and timeouts of external services (usually remote, 3rd party services).

ArturEjsmont 169 Jul 28, 2022
Implementation of the Token Bucket algorithm in PHP.

Token Bucket This is a threadsafe implementation of the Token Bucket algorithm in PHP. You can use a token bucket to limit an usage rate for a resourc

null 477 Jan 7, 2023
A PHP implementation of the Unleash protocol aka Feature Flags in GitLab.

A PHP implementation of the Unleash protocol aka Feature Flags in GitLab. This implementation conforms to the official Unleash standards and implement

Dominik Chrástecký 2 Aug 18, 2021
An implementation of the Minecraft: Bedrock Edition protocol in PHP

BedrockProtocol An implementation of the Minecraft: Bedrock Edition protocol in PHP This library implements all of the packets in the Minecraft: Bedro

PMMP 94 Jan 6, 2023
PHP Implementation of PASERK

PASERK (PHP) Platform Agnostic SERialized Keys. Requires PHP 7.1 or newer. PASERK Specification The PASERK Specification can be found in this reposito

Paragon Initiative Enterprises 9 Nov 22, 2022