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!