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

Detailed example of laravel using middleware to record user request logs

WBOY
WBOYforward
2022-04-26 11:53:014062browse

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)

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
 ]);

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;
    }
}

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);
    }
}

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;
    }
}

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;
    }
}

[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!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete