本文实例讲述了Laravel框架实现利用中间件进行操作日志记录功能。分享给大家供大家参考,具体如下:
利用中间件进行操作日志记录过程:
php artisan make:middleware AdminOperationLog
2、生成了文件./app/Http/Middleware/AdminOperationLog.php
<?php namespace App\Http\Middleware; use Closure; use Illuminate\Http\Request; use Illuminate\Support\Facades\Auth; use App\Http\Models\OperationLog; class AdminOperationLog { /** * Handle an incoming request. * * @param \Illuminate\Http\Request $request * @param \Closure $next * @return mixed */ public function handle($request, Closure $next) { $user_id = 0; if(Auth::check()) { $user_id = (int) Auth::id(); } $_SERVER['admin_uid'] = $user_id; if('GET' != $request->method()){ $input = $request->all(); $log = new OperationLog(); # 提前创建表、model $log->uid = $user_id; $log->path = $request->path(); $log->method = $request->method(); $log->ip = $request->ip(); $log->sql = ''; $log->input = json_encode($input, JSON_UNESCAPED_UNICODE); $log->save(); # 记录日志 } return $next($request); } }
3、中间件引入 ./app/Http/Kernel.php
protected $middlewareGroups = [
'web' => [
...
\App\Http\Middleware\AdminOperationLog::class,
...
],
'api' => [
'throttle:60,1',
'bindings',
],
];
此时进行操作时就会记录操作日志
扫码二维码 获取免费视频学习资料
- 本文固定链接: http://phpxs.com/post/6679/
- 转载请注明:转载必须在正文中标注并保留原文链接
- 扫码: 扫上方二维码获取免费视频资料
查 看2022高级编程视频教程免费获取