Home > PHP Framework > Laravel > Detailed example of laravel using middleware to record user request logs

Detailed example of laravel using middleware to record user request logs

WBOY
Release: 2022-04-26 21:13:10
forward
4108 people have browsed it

This article brings you relevant knowledge about laravel, which mainly introduces related issues about using middleware to record user request logs, including creating middleware, registering middleware, and recording User access and other contents, let’s take a look at them below. I hope it will be helpful to everyone.

Detailed example of laravel using middleware to record user request logs

[Related recommendations: laravel video tutorial]

1. Create middleware

  • You can use the command to create it, or you can create it directly
php artisan make:middleware 中间件名称(TraceRecordMiddleware)
Copy after login

2. Registration middleware

  • Because different versions of laravel are registered The methods of intermediate price are different. Here we take version 5.6 as an example.
  • Add the following code directly to the project bootstarp/app.php
$app->middleware([
     App\Http\Middleware\TraceRecordMiddleware::class
 ]);
Copy after login

3. Record user access log

  • In fact, the middle price can be understood as a request interceptor
  • So it can intercept the data before the user's request and the response data after the user's request
  • Intermediate price template based on the following
<?php

namespace App\Http\Middleware;

use Illuminate\Http\Request;

/**
 * Class TraceRecordMiddleware
 * @package App\Http\Middleware
 */
class TraceRecordMiddleware
{
    public function handle(Request $request, \Closure $next)
    {
        $response = $next($request);

        return $response;
    }
}
Copy after login

4. Record the data before user response

  • You only need to record which routes the user visited, and It doesn’t matter what data the system returns
<?php

namespace App\Http\Middleware;

use App\Model\SystemTraceRecord;
use Illuminate\Http\Request;

/**
 * Class TraceRecordMiddleware
 * @package App\Http\Middleware
 */
class TraceRecordMiddleware
{
    public function handle(Request $request, \Closure $next)
    {
        
        //插入数据库日志表
        SystemTraceRecord::create([
            &#39;method&#39; => $request->getMethod(),
            'secure' => $request->getScheme(),
            'uri' => $request->getRequestUri(),
            'port' => $request->getPort()
        ]);

        return $next($request);
    }
}
Copy after login

5. Record the data after the user’s response

  • Sometimes you want to know after the user’s request, What data is returned by the system
<?php

namespace App\Http\Middleware;

use App\Model\SystemTraceRecord;
use Illuminate\Http\Request;

/**
 * Class TraceRecordMiddleware
 * @package App\Http\Middleware
 */
class TraceRecordMiddleware
{
    public function handle(Request $request, \Closure $next)
    {
        $response = $next($request);
        
        //响应后插入数据
        SystemTraceRecord::create([
            &#39;data&#39; => json_encode($request->all(), JSON_UNESCAPED_UNICODE),
            'response' => $response->getContent() ?: '',
            'status' => $response->getStatusCode()
        ]);

        return $response;
    }
}
Copy after login

6. Record user details log

  • Sometimes you need to know which users accessed
  • You can also directly obtain session data
<?php

namespace App\Http\Middleware;

use App\Model\SystemTraceRecord;
use Illuminate\Http\Request;

/**
 * Class TraceRecordMiddleware
 * @package App\Http\Middleware
 */
class TraceRecordMiddleware
{
    public function handle(Request $request, \Closure $next)
    {
        $response = $next($request);

        $session = app(&#39;session&#39;);

        SystemTraceRecord::create([
            &#39;user_id&#39; => $session->get('user_info.id', '未知'),
            'username' => $session->get('user_info.username', '未知'),
            'method' => $request->getMethod(),
            'secure' => $request->getScheme(),
            'uri' => $request->getRequestUri(),
            'response' => $response->getContent() ?: '',
            'status' => $response->getStatusCode()
        ]);

        return $response;
    }
}
Copy after login

[Related recommendations: laravel video tutorial]

The above is the detailed content of Detailed example of laravel using middleware to record user request logs. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:csdn.net
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template