LENKRAD - The modern PHP core
Basics
Advanced
Deployment

Events

Events are commonly used to react to code executed later in the process. This is useful both to listen to internal processes, as well as custom defined events.

The Event class

Event is on of the classes used statically.

Main methods

Method
Description

Event::on(string $eventName, callable $callable)

Executes a callable when the defined event is dispatched

Event::dispatch(string $eventName, mixed $payload)

Dispatches an event and triggers registered listeners

Event::onAny(callable $callable)

Executes a callable whenever an event is dispatched

Event::subscribeToClass(string $className, callable $callable)

Fires whenever the notify event is fired from internal classes (Routable & Model updates) or custom classes made available through:

Event::makeListenable(string $className)

Exposes the notifier in a custom class

Generic events

Lenkrad dispatches own events that are used internally, but can be reacted on whenever convenient.

Generic events

Event
Description

DATABASE_ADAPTER_CONNECTED

Fires when a database adapter has been loaded into the app

BEFORE_DATABASE_TRANSACTION

Fires whenever a database transaction is about to happen

AFTER_DATABASE_TRANSACTION

Fires whenever a database transaction happened

BEFORE_RENDERING

Fires before the rendering process begins

REQUEST_HANDLER_INITIALIZED

Fires when the request handler is initialized

REQUEST_HEADERS_SET

Fires when request headers are set

REQUEST_INPUT_PARSED

Fires when input has been parsed into "Input"

ROUTE_HANDLER_INITIALIZED

Fires when the route handler is initialized

ROUTE_REGISTERED

Fires when a route is registered

RESPONSE_HANDLER_SET

Fires when the response handler is set

BEFORE_RESPONSE

Fires before result is handed over to the response handler

ROUTE_INJECTION

Fires when data is injected into a route

BEFORE_ROUTABLE_EXECUTION

Fires before a routable class is executed

Basic examples

Listening to a generic event


            ...

            use Neoan\Event\Event;
            use Neoan\Enums\GenericEvent;

            Event::on(GenericEvent::BEFORE_RESPONSE, function($event){
                if($event['handler'] === 'html'){
                    header('X-Custom-header: web-app');
                }
            });
        

Listening to a custom event


            ...

            use Neoan\Event\Event;

            $logFile = dirname(__DIR__,2) . '/log.txt';

            Event::on('log', function($event){
                $data = [
                    'time' => time(),
                    'event' => $event
                ];
                file_put_contents($logFile, json_encode($data), FILE_APPEND);
            });
        

Dispatching a custom event


            ...

            use Neoan\Event\Event;

            ...

            $event = [
                'msg' => 'Email sent to: ' . $newUser->email
            ];

            Event::dispatch('log', $event);
        

Listening to classes

You can subscribe to particular classes an react to whenever they use their notifier. This can be useful when listing for a particular model, for example.


            ...

            use Neoan\Event\Event;
            use App\Models\UserModel;

            ...

            Event::subscribeToClass(UserModel::class, function($eventName, $class, $arguments){
                // do something
            });

        

You can also make any class listenable like this


            ?php

            use Neoan\Event\Event;
            use Neoan\Event\Listenable;
            use Neoan\Event\EventNotification;

            class AnyClass implements Listenable
            {
                private EventNotification $notifier;

                function __construct()
                {
                    $this->notifier = Event::makeListenable($this);
                }
                function doSomething(string $value)
                {
                    ...
                    $this->notifier->inform($value);
                }
            }

        

Before you move on

Many references on this page assume default settings. Your project might differ in behavior, paths etc.