Use a modern way to centrally register events in your CraftCMS app
- PHP 8.0.2 or later.
Then tell Composer to load the library
composer require panlatent/craft-event-register
The Events Register provides a configuration with annotations to register event handlers in a unified way.
1 Add events.php
to config
directory. This configuration file supports 3 methods:
Function array:
<?php
return [
#[RegisterComponentTypes(Elements::class, Elements::EVENT_REGISTER_ELEMENT_TYPES)]
function(): array {
return [YourElement::class];
},
];
Class object
<?php
return new class {
#[RegisterComponentTypes(Elements::class, Elements::EVENT_REGISTER_ELEMENT_TYPES)]
public function registerElements(): array {
return [YourElement::class];
},
}; // or return new YourClass()
Class config by Yii::createObject()
/ Craft::createObject()
<?php
return ['class' => YourClass::class]
- Register event using annotations class demo
The project is open-sourced software licensed under the MIT license.