如何在yii2中使用事件? [英] How to use events in yii2?

查看:195
本文介绍了如何在yii2中使用事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经遍历了使用Google找到的文档和Yii2 events上的所有文章.有人可以给我一个很好的例子,说明如何在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.

事件基础

事件由以下周期组成.

Events are composed of the following cycle.

  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.

我需要将上述处理程序附加到event.我做的基本方法是使用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.

这篇关于如何在yii2中使用事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆