zoukankan      html  css  js  c++  java
  • 非常简单的方法在你的后台添加《系统管理员操作日志》的功能

    出于监控多用户操作后台的目的,往往需要把各个管理员操作了什么记录下来。

    这个功能用yii2来实现简直是太简单了!下边上代码~

    此demo基于advanced,具体功能可以参考demo 帐号demo 密码111111

    1、在backend目录创建components/AdminLog.php

    <?php
    namespace backendcomponents;
    
    use Yii;
    use yiihelpersUrl;
    
    class AdminLog
    {
        public static function write($event)
        {
            // 具体要记录什么东西,自己来优化$description
            if(!empty($event->changedAttributes)) {
                $desc = '';
                foreach($event->changedAttributes as $name => $value) {
                    $desc .= $name . ' : ' . $value . '=>' . $event->sender->getAttribute($name) . ',';
                }
                $desc = substr($desc, 0, -1);
                $description = Yii::$app->user->identity->username . '修改了' . $event->sender->className() . 'id:' . $event->sender->primaryKey()[0] . '的' . $desc;
                $route = Url::to();
                $userId = Yii::$app->user->id;
                $model = new commonmodelsAdminLog();
                $data = [
                    'route' => $route,
                    'description' => $description,
                    'user_id' => $userId,
                    'created_at' => time(),
                ];
                $model->setAttributes($data);
                $model->save();
            }
        }
    }

    2、在backend/config/main.php添加

    'on beforeRequest' => function($event) {
        yiiaseEvent::on(
            yiidbBaseActiveRecord::className(), 
            yiidbBaseActiveRecord::EVENT_AFTER_UPDATE, 
            ['backendcomponentsAdminLog', 'write']
        );
    },

    3、mysql 中创建admin_log表

    CREATE TABLE `admin_log` (
      `id` int(10) NOT NULL AUTO_INCREMENT,
      `route` varchar(255) NOT NULL DEFAULT '',
      `description` text,
      `created_at` int(10) NOT NULL,
      `user_id` int(10) NOT NULL DEFAULT '0',
      PRIMARY KEY (`id`)
    ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;

    4、用gii生成AdminLog模型:命名空间为commonmodels

    来源:http://www.51siyuan.cn/2.html

  • 相关阅读:
    [Linux] day04——Linux 入门
    react 资源汇总
    画原型图工具
    Atom 插件安装
    react 编写组件 五
    webstom 配置git 后左侧菜单栏配色调整
    Webstorm 不识别es6 import React from ‘react’——webstorm不支持jsx语法怎么办
    Es6 之for of
    一个react的完整项目展示
    前后端分离 接口管理神器——Rap本地搭建
  • 原文地址:https://www.cnblogs.com/grimm/p/5807697.html
Copyright © 2011-2022 走看看