To declare an event in your CComponent child class, you should add a method with aname starting with on. For example, if you add the onRegister method, you will get acorresponding event declared.A method used to declare an event becomes the defaultev…
模型中/** * 在初始化时进行事件绑定 */ public function init() { $this->on(self::EVENT_HELLO,[$this,'sendMail']); $this->on(self::EVENT_HELLO,[$this,'notification']); //and so on ... } public function sendMail($event) { echo 'mail sent to admin'; } public function…
控制器: public function actionTests1(){ $c = new \app\components\cat(); $m = new \app\components\mou; $g = new \app\components\dog; Event::on(\app\components\cat::className(),'miao',[$m,'run']); // Event::on(cat::className(),'miao',function(){echo 11;})…
根据之前一篇文章,我们知道 Yii2 的事件分两类,一是类级别的事件,二是实例级别的事件.类级别的事件是基于 yii\base\Event 实现,实例级别的事件是基于 yii\base\Component 实现. 今天先来看下类级别事件的实现,代码是 yii\base\Event 类. <?php namespace yii\base; /** * Event is the base class for all event classes. */ class Event extends Obje…
https://blog.csdn.net/qq43599939/article/details/80363827 通过观察者来理解yii事件 测试 class TestController extends Controller { const EVENT_MY_EVENT = 'myEvent'; public function actionTest() { echo 'this is test action<br>'; $myEvent = new MyEvent(); $this-&g…
事件(Event)例如:最近的视觉中国'黑洞事件'.我们大多数人(订阅者)是通过XX平台(发布者)得知的这一消息,然后订阅者A出售视觉中国的股票(触发的方法),订阅者B买入视觉中国的股票. using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; namespace ConsoleEvent { class Program { stat…