如何在 yii2 中使用事件?

2022-01-07 00:00:00 php yii2

我已经阅读了使用 Google 找到的有关 Yii2 事件 的文档和所有文章.有人可以为我提供一个很好的例子,说明如何在 Yii2 中使用事件以及它在哪些地方看起来合乎逻辑?

I've been through the documentation and all the articles on Yii2 events found using Google. Can someone provide me a good example of how events can be used in Yii2 and where it may seem logical?

推荐答案

我可以通过一个简单的例子来解释事件.比方说,当用户第一次注册网站时,您希望做一些事情,例如:

I can explain events by a simple example. Let's say, you want to do few things when a user first registers to the site like:

  1. 向管理员发送电子邮件.
  2. 创建通知.
  3. 你说出它的名字.

在成功保存用户对象后,您可以尝试调用一些方法.也许是这样的:

You may try to call a few methods after a user object is successfully saved. Maybe like this:

if($model->save()){
   $mailObj->sendNewUserMail($model);
   $notification->setNotification($model);
}

到目前为止看起来还不错,但是如果需求的数量随着时间的推移而增长呢?说用户注册后必须发生的 10 件事?在这种情况下,事件会派上用场.

So far it may seem fine but what if the number of requirements grows with time? Say 10 things must happen after a user registers himself? Events come handy in situations like this.

活动基础

事件由以下循环组成.

  1. 您定义一个事件.比如说,新用户注册.
  2. 您可以在模型中为其命名.也许在 User 模型中添加常量.像const EVENT_NEW_USER='new_user';.这用于添加处理程序和触发事件.
  3. 您定义了一个在事件发生时应该执行某些操作的方法.例如,向管理员发送电子邮件.它必须有一个 $event 参数.我们将此方法称为处理程序.
  4. 您可以使用名为 on() 的方法将该处理程序附加到 model.您可以根据需要多次调用此方法 - 只需将多个处理程序附加到单个事件即可.
  5. 您可以使用 trigger() 触发事件.
  1. You define an event. Say, new user registration.
  2. You name it in your model. Maybe adding constant in User model. Like const EVENT_NEW_USER='new_user';. This is used to add handlers and to trigger an event.
  3. You define a method that should do something when an event occurs. Sending an email to Admin for example. It must have an $event parameter. We call this method a handler.
  4. You attach that handler to the model using its a method called on(). You can call this method many times you wish - simply you can attach more than one handler to a single event.
  5. You trigger the event by using trigger().

请注意,上面提到的所有方法都是Component 类的一部分.Yii2 中几乎所有的类都继承自这个类.是的 ActiveRecord 也是.

Note that, all the methods mentioned above are part of Component class. Almost all classes in Yii2 have inherited form this class. Yes ActiveRecord too.

让我们编码

为了解决上述问题,我们可能有 User.php 模型.我不会在这里写所有的代码.

To solve above mentioned problem we may have User.php model. I'll not write all the code here.

// in User.php i've declared constant that stores event name
const EVENT_NEW_USER = 'new-user';

// say, whenever new user registers, below method will send an email.
public function sendMail($event){
   echo 'mail sent to admin';
   // you code 
}

// one more hanlder.

public function notification($event){
  echo 'notification created';
}

这里要记住的一件事是,您不必在创建事件的类中创建方法.您可以从任何类中添加任何静态、非静态方法.

One thing to remember here is that you're not bound to create methods in the class that creates an event. You can add any static, non static method from any class.

我需要将上述处理程序附加到事件.我所做的基本方法是使用 AR 的 init() 方法.所以方法如下:

I need to attach above handlers to the event . The basic way I did is to use AR's init() method. So here is how:

// this should be inside User.php class.
public function init(){

  $this->on(self::EVENT_NEW_USER, [$this, 'sendMail']);
  $this->on(self::EVENT_NEW_USER, [$this, 'notification']);

  // first parameter is the name of the event and second is the handler. 
  // For handlers I use methods sendMail and notification
  // from $this class.
  parent::init(); // DON'T Forget to call the parent method.
}

最后一件事是触发一个事件.现在您不需要像以前那样显式调用所有必需的方法.您可以通过以下方式替换它:

The final thing is to trigger an event. Now you don't need to explicitly call all the required methods as we did before. You can replace it by following:

if($model->save()){
  $model->trigger(User::EVENT_NEW_USER); 
}

将自动调用所有处理程序.

All the handlers will be automatically called.

相关文章