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.'); } }