Laravel framework implements operation logging function using middleware

不言
Release: 2023-03-30 10:52:02
Original
2448 people have browsed it

This article mainly introduces the Laravel framework to implement the operation logging function using middleware. It analyzes the creation and introduction of the Laravel framework middleware and the related implementation techniques of using the middleware for the operation logging function in the form of examples. What is needed Friends can refer to

. This article describes the example of the Laravel framework implementing the operation logging function using middleware. Share it with everyone for your reference, the details are as follows:

Use middleware for operation logging process:

1. Create middleware

php artisan make:middleware AdminOperationLog
Copy after login

2. The file./app/Http/Middleware/AdminOperationLog.php

is generated. The code is as follows:

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); } }
Copy after login

##3. Introduction of middleware./app/Http/Kernel.php

protected $middlewareGroups = [ 'web' => [ ... \App\Http\Middleware\AdminOperationLog::class, ... ], 'api' => [ 'throttle:60,1', 'bindings', ], ];
Copy after login

The operation log will be recorded when the operation is performed at this time

Related recommendations:

Laravel framework implementation utilization The listener performs sql statement recording function

The above is the detailed content of Laravel framework implements operation logging function using middleware. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!