Overview
ForkBB uses an event system that allows extensions to hook into various points in the application lifecycle. Events are dispatched at key moments, and extensions can register listeners to respond to these events.How Events Work
The event system consists of three main components:- Event - Object containing event data and state
- EventDispatcher - Manages and dispatches events to listeners
- EventListener - Base class for listeners that respond to events
The Event Class
Events extendstdClass and provide control over propagation:
stdClass:
The EventDispatcher
The EventDispatcher loads event listeners and dispatches events:Creating Event Listeners
To create an event listener, extend theEventListener base class:
$eventList property maps event names to method names in your listener.
Registering Listeners
Register listeners in your extension’scomposer.json:
Available Events
ForkBB dispatches events at various points in the application lifecycle:bootstrap:start
Dispatched at the very beginning of the bootstrap process, before routing:- Adding custom controllers
- Early initialization
- Request preprocessing
Models\Page:prepare:after
Dispatched after a page model is prepared but before rendering:- Modifying page data
- Adding navigation items
- Injecting custom variables
Models\Pages\Admin:prepare:after
Dispatched after admin page preparation:- Adding admin menu items
- Customizing admin interface
- Admin-specific modifications
Event Propagation
Listeners can stop event propagation to prevent subsequent listeners from running:stopPropagation() is called, no further listeners will be executed for this event.
Event Priority
Priority determines the order listeners are called. Higher values run first:Extension1\Listener runs before Extension2\Listener.
From the Extensions manager:
Event Configuration
When extensions are enabled, event mappings are compiled intoapp/config/ext/events.php:
Example: Adding Custom Navigation
Here’s a complete example of using events to add a custom navigation item:Best Practices
Return boolean from listener methods
Return boolean from listener methods
Always return
true on success or false on failure. This maintains consistency with the event system.Don't modify core events
Don't modify core events
Never modify the event object’s core properties unless documented. Add your own custom properties instead.
Use appropriate priority values
Use appropriate priority values
Use priority values between 0-1000. Reserve very high values (900+) for critical early initialization.
Handle missing data gracefully
Handle missing data gracefully
Always check if event properties exist before accessing them, as other extensions may modify event data.
Document your events
Document your events
If your extension dispatches custom events, document them for other developers to use.
Debugging Events
To debug event flow:- Check
app/config/ext/events.phpto see registered listeners - Add logging in your listener methods
- Use
var_dump($event)to inspect event data - Check extension is enabled in admin panel
Next Steps
Creating Extensions
Learn more about creating complete extensions