1. 创建数据表迁移脚本
2. 编辑迁移文件
3. 执行迁移
4. 操作记录日志列表显示和详情显示的MVC
5. 在具体需要记录日志的地方调用
1.创建数据库迁移脚本
docker-compose run --rm app ./yii migrate/create --migrationPath=@common/migrations add_admin_log_table
2. 编辑迁移文件
<?php use yii\db\Schema; use yii\db\Migration; class m151109_101640_add_admin_log_table extends Migration { public function up() { $tableOptions = null; if ($this->db->driverName === 'mysql') { $tableOptions = 'CHARACTER SET utf8 COLLATE utf8_general_ci ENGINE=InnoDB COMMENT="后台操作记录"'; } $this->createTable('{{%admin_log}}', [ //'name'=>Schema::TYPE_STRING.'(200) PRIMARY KEY NOT NULL', 'id'=>Schema::TYPE_PK, 'admin_id'=>Schema::TYPE_INTEGER.'(11) UNSIGNED NOT NULL COMMENT "操作用户ID"', 'admin_name'=>Schema::TYPE_STRING.'(200) NOT NULL COMMENT "操作用户名"', 'addtime'=>Schema::TYPE_INTEGER.'(11) NOT NULL COMMENT "记录时间"', 'admin_ip'=>Schema::TYPE_STRING.'(200) NOT NULL COMMENT "操作用户IP"', 'admin_agent'=>Schema::TYPE_STRING.'(200) NOT NULL COMMENT "操作用户浏览器代理商"', 'title'=>Schema::TYPE_STRING.'(200) NOT NULL COMMENT "记录描述"', 'controller'=>Schema::TYPE_STRING.'(200) NOT NULL COMMENT "操作模块(例:文章)"', 'action'=>Schema::TYPE_STRING.'(200) NOT NULL COMMENT "操作类型(例:添加)"', 'handle_id'=>Schema::TYPE_INTEGER.'(11) NOT NULL COMMENT "操作对象ID"', 'result'=>Schema::TYPE_TEXT.' NOT NULL COMMENT "操作结果"', 'describe'=>Schema::TYPE_TEXT.' COMMENT "备注"', ], $tableOptions); } public function down() { $this->dropTable('{{%admin_log}}'); } }
3. 执行迁移
docker-compose run --rm app ./yii migrate --migrationPath=@common/migrations
4. 操作记录日志列表显示和详情显示的MVC
4.1 Model
<?php namespace common\models; use Yii; /** * This is the model class for table "admin_log". * * @property integer $id * @property string $title * @property string $addtime * @property integer $admin_name * @property integer $admin_ip * @property integer $admin_agent * @property integer $controller * @property integer $action * @property integer $objId * @property integer $result */ class AdminLog extends \yii\db\ActiveRecord { /** * @inheritdoc */ public static function tableName() { return '{{%admin_log}}'; } /** * @inheritdoc */ public function attributeLabels() { return [ 'id'=>'操作记录ID', 'title'=>'操作记录描述', 'addtime'=>'记录时间', 'admin_name'=>'操作人姓名', 'admin_ip'=>'操作人IP地址', 'admin_agent'=>'操作人浏览器代理商', 'controller'=>'操作控制器名称', 'action'=>'操作类型', 'handle_id'=>'操作数据编号', 'result'=>'操作结果', ]; } public static function saveLog($result,$handle_id){ $model = new self; $model->admin_ip = Yii::$app->request->userIP; $headers = Yii::$app->request->headers; $model->addtime = time(); if ($headers->has('User-Agent')) { $model->admin_agent = $headers->get('User-Agent'); } $model->admin_id = Yii::$app->user->identity->id; $model->admin_name = Yii::$app->user->identity->username; $model->controller = Yii::$app->controller->id; $model->action = Yii::$app->controller->action->id; $model->result = $result; $model->handle_id = $handle_id; $model->title = $model->admin_name.' '.$model->action.' '.$model->controller; $model->save(false); } }
4.2 View admin-log/index.php
<?php use yii\grid\GridView; /* @var $this yii\web\View */ /* @var $dataProvider yii\data\ActiveDataProvider */ $this->title = '操作记录'; $this->params['breadcrumbs'][] = $this->title; ?> <div class="handle-index"> <?= GridView::widget([ 'dataProvider' => $dataProvider, 'columns' => [ 'title', [ 'attribute'=>'addtime', 'value'=>function($model){ return date('Y-m-d H:i:s',$model->addtime); }, ], ['class' => 'yii\grid\ActionColumn','template'=>'{view}'] ], 'tableOptions'=>['class' => 'table table-striped'] ]); ?> </div>
4.3 View admin-log/view.php
<?php use yii\widgets\DetailView; /* @var $this yii\web\View */ /* @var $model backend\models\Admin */ $this->title = '操作记录: '.$model->title; $this->params['breadcrumbs'][] = ['label' => '操作记录', 'url' => ['index']]; $this->params['breadcrumbs'][] = $this->title; ?> <div class="admin-view"> <?= DetailView::widget([ 'model' => $model, 'attributes' => [ 'id', 'admin_name', 'addtime:datetime', 'admin_ip', 'admin_agent', 'controller', 'action', 'handle_id', 'result' ], ]) ?> </div>
4.4 Controller AdminLogController
<?php /** * Created by PhpStorm. * User: michaeldu * Date: 15/11/9 * Time: 下午6:21 */ namespace app\controllers; use common\models\AdminLog; use yii\data\ActiveDataProvider; class AdminLogController extends \yii\web\Controller { public function actionIndex() { $dataProvider = new ActiveDataProvider([ 'query' => AdminLog::find(), 'sort' => [ 'defaultOrder' => [ 'addtime' => SORT_DESC ] ], ]); return $this->render('index',[ 'dataProvider' => $dataProvider ]); } public function actionView($id){ return $this->render('view',[ 'model'=>AdminLog::findOne($id), ]); } }
5. 在控制器中调用日志记录
public function actionCreate() { $model = new Banner(); $model->status=Banner::STATUS_DISPLAY; if ($model->load(Yii::$app->request->post()) && $model->save()) { //保存操作记录 \common\models\AdminLog::saveLog($model->searchById($model->primaryKey),$model->primaryKey); Yii::$app->session->setFlash('success','Banner【'.$model->title.'】发布成功'); return $this->redirect(['index']); } else { return $this->render('create', [ 'model' => $model, ]); } } public function searchById($id){ if (($model = Banner::findOne($id)) !== null) { return json_encode($model->toArray()); } else { throw new \yii\web\NotFoundHttpException('The requested page does not exist.'); } }
相关推荐
yii 2.0通用后台系统管理,实现注册登录上传用户自定义头像
带权限管理的yii2.0后台框架,适合任何情况,最新的值得使用
使用SeasLog高效日志扩展替换Yii2框架的日志模块,使其提高写日志效率。只需配置就可以实现。
增加后台管理员操作日志记录 测试了js和css合并压缩,只是把后台各页面通用的合并了一下,生产环境下,如果第一次或者是修改了合并中引用的js或css,需要运行命令yii asset backend/assets....
yii简易后台基础模板
基于Yii2+layui的后台框架模板,实现了完善的RBAC权限控制-yii2-admin-theme
yii2框架实现验证码的添加以及验证
系统基于yii2高级版本开发,后台模板使用的 Ace Admin。对于一般的后台开发,比较方便; 对于数据表的CURL操作都有封装,且所有操作都有权限控制。现在这个项目只是一个示例项目, 主要功能被我移到: [jinxing/yii2-...
yii2-oauth2-server, 用于实现OAuth2服务器的包装器( https yii2-oauth2-server用于实现OAuth2服务器的包装器( https://github.com/bshaffer/oauth2-server-php 插件)安装安装这里扩展的首选方法是通过 Composer 。...
YII2框架整合H+后台UI框架,方便开发后台,框架上作了些调整,本地使用web-local.php配置,子模块配置继承父模块的
支付宝支付sdk及demo,一键安装搭建,方便部署;丰富的插件和各种自定义菜单配置,特别适用于快速开发的中小型企业
yii2-shop, 使用 Yii 2.0实现简单商店的示例项目 Yii 2商店示例项目这是实现一个商店来帮助人们学习 Yii 2.0的例子项目。 它是在 workshop 在Ekaterinburg中执行 8小时时创建的。 这个想法是展示如何处理,网格,...
Yii2参考手册,Yii2中文手册完整版
yii2后台,可用于网站后台界面的建设,简介,明了的界面!!!
yii2 创建表单 笔记
Yii2.0级基于Yii1.X完全重写的,不仅是小版本升级的升级。所以想要系统学习Yii2.0这个框架这套教程可以从实战项目带你入手。不错的学习YII框架的基础课程,电商系统的结构完整、框架资源太大,传百度网盘了,链接在...
yii2-wechat-sdk 是基于 Yii2 实现的微信API(公众号,企业号)高效率(绝对的!)SDK. 目前有3个主要文件可以使用Wechat.php 旧版微信公众号操作类(在新版[1.0]发布后会删除)MpWechat.php 新版微信公众号操作类(更标准,...
基于YII2框架的CMS, 预览地址http://demo.cms.feehi.com
yii2 swoole:让yii2运行在swoole上 , 运行在swoole上的yii2是运行在php-fpm上yii2的5倍以上
主要介绍了Yii2搭建后台并实现rbac权限控制完整实例教程的相关资料,需要的朋友可以参考下