I learn about events from yii2 doc.
I know how it works, but i don't know where to use it and how to use it in my development.There is example of send email notification but i want a solid example which clear the idea where to use and how to use it .
MY code is below in model i write
const EVENT_NEW_USER = 'new-user';
public function sendMailto($event){
$this->sendMail(arguments);
// you code
}
in controller:
use yii\base\Component;
use yii\base\Event;
public function someaction (){
$model->on(SignUpForm::EVENT_NEW_USER, [$model,'sendMailto'],['auth'=>$model_auth,'user_details'=>$user_details]);
$model->trigger(SignUpForm::EVENT_NEW_USER);
}
I use events in Yii for loosely coupling sender and receiver of messages inside a single Yii installation (no distrubution, single server).
In my Yii software there are Yii modules for feature blocks. Customers get module sets depending on their feature requirements. These module need to commuicate with each other.
Example:
In order to make sender modules independend of receiver modules, i use the Yii event mechanisms. So, Module 1 does not need to know anything about the receiver and Modules 2 and 3 do not need anything about the sender. There is just a common message structure.
That works perfectly and the modules are mutually independend.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With