Yii2 在任何用户创建或更新后向管理员发送消息(或通知)

1 yii2

三周前,我试图找到一种在任何用户创建或更新后向管理员发送消息(或通知)的方法,但最终一无所获。我搜索了很多,但没有找到明确的解决方案,我试图了解 Yii2 事件,我找到了这个链接 http://www.yiiframework.com/wiki/329/real-time-display-of-server-push -data-using-server-sent-events-sse/

我认为这是解决我问题的关键,但我真的被困住了我不知道该怎么办,希望任何人都可以帮助我。

谢谢

Dil*_*rth 5

考虑使用行为来处理这个问题。

假设

  • 您的项目中至少有一个模型(可能有多个)。
  • 您有一个包含至少两个操作的控制器:actionCreate 和 actionUpdate。
  • 每当调用上述任一操作时,都会向管理员发送一封电子邮件。

事件和行为

当 actionCreate 被调用时,一条新记录通过一个扩展ActiveRecord的模型类的实例插入到数据库中。类似地,当 actionUpdate 被调用时,从数据库中获取现有记录,更新并保存回来。在这两种情况下,模型都会触发一个事件(即:插入或更新)(因为模型扩展了组件并且组件负责实现事件)。Yii2 提供了使用“自定义组件的正常代码执行”行为来响应这些事件的能力。

简而言之,这意味着您可以将自定义代码绑定到任何给定的事件,以便在触发事件时执行您的代码

建议的解决方案

现在我们对事件和行为有了一些了解,我们可以创建一个行为,在触发插入或更新事件时执行一些自定义代码。这个自定义代码可以检查被调用的操作的名称(它被称为创建还是更新?)以确定是否需要发送电子邮件。

但是,该行为本身是无用的,我们需要将其附加到任何应该触发它的模型上。

解决方案的实施

通知行为.php

<?php

namespace app\components;

use yii\base\Behavior;
use yii\db\ActiveRecord;

class NotificationBehavior extends Behavior
{

  /**
   * Binds functions 'afterInsert' and 'afterUpdate' to their respective events.
   */
  public function events()
  {
    return [
      ActiveRecord::EVENT_AFTER_INSERT => 'afterInsert',
      ActiveRecord::EVENT_AFTER_UPDATE => 'afterUpdate',
    ];
  }

  /**
   * This function will be executed when an EVENT_AFTER_INSERT is fired
   */
  public function afterInsert($event)
  {
    // check the 'id' (name) of the action
    if (Yii::$app->controller->action->id === 'create') {
      // send email to administrator 'user performed insert'
    }
  }

  /**
   * This function will be executed when an EVENT_AFTER_UPDATE is fired
   */
  public function afterUpdate($event)
  {
    if (Yii::$app->controller->action->id === 'update') {
      // send email to administrator 'user performed update'
    }
  }

}
Run Code Online (Sandbox Code Playgroud)

后控制器.php

<?php

namespace app\controllers;

use Yii;
use app\models\Post;
use yii\web\Controller;
use yii\web\NotFoundHttpException;

class PostController extends Controller
{
    /**
     * Creates a new record
     */
    public function actionCreate()
    {
        $model = new Post;

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['view', 'id' => $model->id]);
        } else {
            return $this->render('create', [
                'model' => $model,
            ]);
        }
    }

    /**
     * Updates an existing record
     */
    public function actionUpdate()
    {
        // ...
    }

}
Run Code Online (Sandbox Code Playgroud)

Post.php(模型)

<?php

namespace app\models;

use app\components\NotificationBehavior;
use yii\db\ActiveRecord;

class Post extends ActiveRecord
{
  /**
   * specify any behaviours that should be tied to this model.
   */
  public function behaviors()
  {
      return [
          // anonymous behavior, behavior class name only
          NotificationBehavior::className(),
      ];
  }
}
Run Code Online (Sandbox Code Playgroud)

我还建议查看Yii2 的 TimestampBehavior 实现以获得更具体的示例。