Eventful – A CakePHP Event System
As promised in the comments of the most recent entry i created a repo for my event system.
It’s currently under heavy development, yet ready to use. I don’t think the interface will change much from here on out. The installation is similar to the DebugKit. Put the “eventful” plugin into your plugins folder and add the Behavior or Component to your app. This will include all dependencies and the system is ready to use.
Only one thing is different from regular plugins. There is a seperate “events” folder which is a empty layout for the system and the place for your event listener classes.
The System is also plugin aware. If you want to enrich your base application with event handlers from plugins just copy the “events” folder layout into the plugin directory and prefix the class filenames with the plugin name. (just like app_controller).
# /app/plugins/pizza/events/controller/pizza_users_controller_events.php class PizzaUsersControllerEvents extends AppControllerEvents ...
I’ve created a (messy) README which goes on about how to use it. Basicly the workflow is: a) add the component/behavior, b) use the dispatch/dispatchEvent method in your actions/methods, c) create or modify the event listener class and add a matching “on” method. d) go nuts.
class UsersController extends AppController {
var $components = array('Eventful.Event');
}
class UsersController extends AppController {
var $components = array('Eventful.Event');
function logout() {
$this->Event->dispatch('UserLogout', array(
'auth' => $this->Auth->user()
));
}
}
File: /app/events/controller/users_controller_events.php
class UsersControllerEvents extends AppControllerEvents {
function onUserLogout() {
$this->log('User logged out...')
}
}
Unlike the original implementation the plugin triggers all handlers of the same name. So if you have a “userLogout” event fired from your UsersController::logout() action the dispatchEvent method will trigger all possible candidates. Regardless in which listener class the method is.
class MembersControllerEvents extends AppControllerEvents {
function onUserLogout($event) {
if ($event->auth['isMember'])
$this->log('Member logged out...')
}
}
Enjoy. You can find it on github:
http://github.com/m3nt0r/eventful-cakephp


Love the concept but didn’t try it _yet_. Thanks for your work, watching the Repo for sure!
[...] put together a cool looking plugin for handing events in your application. This seems like a good way to organize your code and would be useful if you [...]
This seems really kick ass…. Why aren’t more people interested in this? I think I may be using this very heavily in my cms to implement plugins or it :).
Thanks for your hard work!
I have been using this a lot to facilitate communication between the main app and a list of plugins that *might* be installed.
One thing, svn .empty files in model / controller folders or in the event subfolders will cause it to throw errors.
Wow.
Just nice!
Sadly I haven’t a chance to properly try this yet. I’m planning on modular CRM system and events make implementing modules extremely easy so this seems perfect. However I do have few key questions about the implementation (I could dig in the source but that wouldn’t help others).
1. Have you run any performance tests? I’d assume the listeners are cached so the events should have minimal overhead. On the other hand loading the actual listeners isn’t exactly fast.
2. Can the listeners call controllers? If it’s possible an example of that would be nice.
Very nice! Just what I needed and in a beatiful way. Already using It.
I just got it working after missed the bit about putting the event handlers in the app/event/controller folder (not app/controller) and deleting the “empty” file.
But now this looks really useful for things like handling page stat recording, user-account set-up, etc. This is the best plug-in I’ve seen for Cake in a while – thanks a lot!
small typo:
File: /app/events/controller/users_controller_events.php
should read:
File: /app/events/controllers/users_controller_events.php
License?