CakePHP event mechanism early adopters

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.