Events & hooks & friends

I’d like to add “actions to happen when conditions are met” into a slim app. After spending 2 hours googling keywords such as events, hooks, triggers, observers, ddd, etc. I eventually got lost in the terminology. I’m not a fluent developer so I’m hoping for advice directing me in the right direction.

I got a MVC structured Slim 4 base. On the model side, the functions do only validation and data handling (via PHP-MySQLi-Database-Class which I prefer over full blown ORMs). My goal is to

  • trace users’ activity (i.e. database inserts and updates, file uploads and deletes)
  • log authentication attempts
  • eventually send realtime notifications in a far far feature (i.e. via ratchet or similar)

My trivial approach would be to write a global “event emitter” function which I’d type in manually into all class methods that handle data. This “event emitter” would store events and maybe some payload data in, say, redis or similar. On each app run, there’d be an “event listener” which would run some closures/methods as configured.

Now this surely has been done many times before. I assume, that there’s a simple way / usual, how I could run a callback before a function/method returns (without manually typing out the event emitter function everywhere) such as with https://pecl.php.net/package/uopz. But since I didn’t see this used in such a way, I assume it has some drawback coming along (i.e. a performance hit, not being as flexible as I imagine it to be, etc.) so I started looking all over to finally get overwhelmed with too much information :slight_smile:

Any tips, recommendations? Are there existing packages/middleware & some example code with Slim?
Thanks lots!

To me it sounds like you want logging, perhaps Monolog or a wrapper around your DB-class autowired into the appropriate classes. If you want to add a notification system you can also write to a Redis instance and have your clients subscribe to it over websocket.

@killua Have you tried the Symfony EventDispatcher?