This article mainly introduces the Laravel framework realizes the function of operation logging using middleware, analyzes the creation and introduction of Laravel framework middleware and the related implementation techniques of using middleware to operate logging function, and the need of friends can refer to the following
In this paper, the Laravel framework is used to realize the operation logging function using middleware. Share to everyone for your reference, as follows:
The process of logging operations using middleware:
1. Create middleware
PHP Artisan Make:middleware Adminoperationlog
2. Generated file ./app/http/middleware/adminoperationlog.php
The code is as follows:
<?phpnamespace 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 (); # Create a table in advance, 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 (); # log } return $next ($request);} }
3. Middleware Introduction ./app/http/kernel.php
protected $middlewareGroups = [ ' web ' = [ ... \app\http\middleware\adminoperationlog::class, ... ], ' api ' = = [ ' throttle:60,1 ', ' Bindings ', ], ];
The operation log is logged at this time