Getting started
Routing and controllers
Command line
Databases (SQL)
Databases (NoSQL)
Security
Packages
Learn more
- Array helper
- Caching
- Collections
- Command bus (deprecated)
- Command, event and query buses
- Date and time
- Events (deprecated)
- File system
- HTML helper
- Humanizer
- Image manipulation
- Internationalization
- Logging
- Number helper
- Pagination
- Retry helper
- Sessions
- String helper
- URL builder
- UUID helper
- Validation
- Views
Official packages
Events
Mako includes two ways of handling events, an event listener and a trait that makes your class observable.
The event library is deprecated and will be removed in Mako 10. It is replaced by the new bus library.
Event listener
The register
method lets you register an event handler that will get executed when the event is triggered. You can register multiple handlers for the same event. They will be executed in the order that they were registered.
$this->event->register('foobar', fn () => 'foobar event handler');
You can also handle your events using a class instead of a closure.
$this->event->register('foobar', FoobarHandler::class);
Class handlers will be instantiated by the dependency injection container so you can easily inject your dependencies through the constructor. Both closure and class handlers will be executed by the Container::call()
and all dependencies will automatically be injected there as well.
<?php
namespace app\events;
use mako\event\EventHandlerInterface;
class FoobarHandler implements EventHandlerInterface
{
public function handle()
{
return 'foobar event handler';
}
}
The has
method will return true
if an event handler has been registered and false
if not.
$registered = $this->event->has('foobar');
The clear
method will clear all event handlers registered for an event.
$this->event->clear('foobar');
The override
method will clear all previously registered handlers for an event before registering a new handler.
$this->event->override('foobar', fn () => 'foobar event 1');
The trigger
method executes all handlers for the registered event and returns an array containing all the return values.
$values = $this->event->trigger('foobar');
You can also pass arguments your handlers using the optional second parameter.
$values = $this->event->trigger('foobar', [1, 2, 3]);
The third optional parameter lets you stop event handling if one of the handlers return false
.
$values = $this->event->trigger('foobar', [1, 2, 3], true);