這篇文章主要介紹了Laravel框架實現利用中間件進行操作日誌記錄功能,結合實例形式分析了Laravel框架中間件的創建、引入以及使用中間件進行操作日誌記錄功能的相關實現技巧,需要的朋友可以參考下
本文實例講述了Laravel框架實作利用中間件進行操作日誌記錄功能。分享給大家供大家參考,具體如下:
利用中間件進行操作日誌記錄流程:
1、建立中間件
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', ], ];
此時進行操作時就會記錄操作日誌
#相關推薦:
#
以上是Laravel框架實作利用中間件進行操作日誌記錄功能的詳細內容。更多資訊請關注PHP中文網其他相關文章!