CakePHP is a very old frame, early adopters are far-fetched, but even the most advanced frameworks such as yii have these mechanisms, but still do not understand
Manual Address: http://book.cakephp.org/2.0/en/core-libraries/events.html
How to use:
1. Use the Cakeeventlistener interface in classes that need to support events (including, models, controllers, etc.) and implement all the methods specified by the interface.
<?Phpapp:: Uses (' Appmodel ', ' Model ') ); APP:: Uses (' Cakeeventlistener ', ' Event ');classCOrderextendsAppmodelImplementsCakeeventlistener { Public functionimplementedevents () {return Array( ' Model.COrder.test ' =Array( ' Callable ' = ' eventtest ', ' passparams 'true ), ); } Public functionEventtest ($order) { //TODOpr$order);Exit(); }}
2, registration events, registration events can be operated in AppController, global registration of all events
// Registering Events $this->loadmodel ("COrder"); $this->geteventmanager ()->attach ($this->corder);
3, triggering events, can be used anywhere in the system trigger event mechanism, using the following methods
// Triggering events $event New $this Array ( ' order ' = =array(' a ')) ; $this->geteventmanager ()->dispatch ($event);
Experience: Using the event mechanism can be decoupled to a certain extent, do not have to introduce the required amount of objects every time, such as the introduction of other models in the model is inappropriate and inconvenient, but the PHP event mechanism caused by the program code is not conducive to tracking, program readability is broken, but also can not be very good transaction operations. So be careful with CakePHP's event mechanism.
CakePHP event mechanism early adopters