中间件调用路由两次,同时在 laravel 的数据库中记录访问日志
Middleware is calling route twice while keeping record of accessing logs in db in laravel
我创建了一个中间件来检查每个 url 并将访问日志数据保存在数据库中。一切顺利,但每次在浏览器中加载 url 时,数据都会在数据库中保存两次。我正在使用 laravel 8 这个项目将用于用户从商店购买优惠券。
App\Http\Middleware\AccessLog.php
<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Http\Request;
use App\Models\LogRecord;
use Auth;
class AccessLog
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle(Request $request, Closure $next)
{
if(strpos($request->path(), 'shop') !== false){
$data['type'] = 'shop';// 'shop' or 'coupon' and for other type 'page' )
$data['content'] = $request->route('id');//content -> shop.id or coupon.id. and other page is route path
}
elseif(strpos($request->path(), 'coupon') !== false){
$data['type'] = 'coupon';
$data['content'] = $request->route('id');
}
else{
$data['type'] = 'page';
$url_components = parse_url($request->path());
$path = explode('/',$url_components['path']);
$data['content'] = $path[0];
}
$user = null;
if (Auth::check()){
$user = Auth::user()->id;
}
$data['user_id'] = $user;
$data['servicer_id'] = config('const.common.accesslog.servicer_id');
$accesslog = LogRecord::create($data);
return $next($request);
}
}
App\Http\Kernel.php
protected $routeMiddleware = [
'auth' => \App\Http\Middleware\Authenticate::class,
'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
'cache.headers' => \Illuminate\Http\Middleware\SetCacheHeaders::class,
'can' => \Illuminate\Auth\Middleware\Authorize::class,
'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
'password.confirm' => \Illuminate\Auth\Middleware\RequirePassword::class,
'signed' => \Illuminate\Routing\Middleware\ValidateSignature::class,
'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
'verified' => \Illuminate\Auth\Middleware\EnsureEmailIsVerified::class,
'log' => \App\Http\Middleware\AccessLog::class,
];
App\Models\LogRecord.php
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class LogRecord extends Model
{
use HasFactory;
protected $table = 'access_logs';
protected $fillable =['user_id','servicer_id','type','content'];
}
数据库:
access_logs table
每次记录保存两次。可能是什么问题?
从您的数据库屏幕截图来看,似乎在几秒钟后添加了重复记录,即。 2-3 秒。所以任何其他请求即。 post 或 ajax 在调用页面后处理。所以请尝试下面的中间件代码 App\Http\Middleware\AccessLog.php
<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Http\Request;
use App\Models\LogRecord;
use Auth;
class AccessLog
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle(Request $request, Closure $next)
{
if($request->method()=='GET')
{
if(strpos($request->path(), 'shop') !== false){
$data['type'] = 'shop';// 'shop' or 'coupon' and for other type 'page' )
$data['content'] = $request->route('id');//content -> shop.id or coupon.id. and other page is route path
}
elseif(strpos($request->path(), 'coupon') !== false){
$data['type'] = 'coupon';
$data['content'] = $request->route('id');
}
else{
$data['type'] = 'page';
$url_components = parse_url($request->path());
$path = explode('/',$url_components['path']);
$data['content'] = $path[0];
}
$user = null;
if (Auth::check()){
$user = Auth::user()->id;
}
$data['user_id'] = $user;
$data['servicer_id'] = config('const.common.accesslog.servicer_id');
$accesslog = LogRecord::create($data);
}
return $next($request);
}
}
谢谢https://whosebug.com/users/16174785/nitrin0
我从 nitrin0 了解到,有时 plugin/extension 可能会影响。所以我试着一个一个地禁用我的每个扩展。在我从我的 google chrome 浏览器中禁用以下扩展 HTML 标签检测器|HTML TAG CHECKER 后它起作用了。太出乎意料了!!
extension
我创建了一个中间件来检查每个 url 并将访问日志数据保存在数据库中。一切顺利,但每次在浏览器中加载 url 时,数据都会在数据库中保存两次。我正在使用 laravel 8 这个项目将用于用户从商店购买优惠券。
App\Http\Middleware\AccessLog.php
<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Http\Request;
use App\Models\LogRecord;
use Auth;
class AccessLog
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle(Request $request, Closure $next)
{
if(strpos($request->path(), 'shop') !== false){
$data['type'] = 'shop';// 'shop' or 'coupon' and for other type 'page' )
$data['content'] = $request->route('id');//content -> shop.id or coupon.id. and other page is route path
}
elseif(strpos($request->path(), 'coupon') !== false){
$data['type'] = 'coupon';
$data['content'] = $request->route('id');
}
else{
$data['type'] = 'page';
$url_components = parse_url($request->path());
$path = explode('/',$url_components['path']);
$data['content'] = $path[0];
}
$user = null;
if (Auth::check()){
$user = Auth::user()->id;
}
$data['user_id'] = $user;
$data['servicer_id'] = config('const.common.accesslog.servicer_id');
$accesslog = LogRecord::create($data);
return $next($request);
}
}
App\Http\Kernel.php
protected $routeMiddleware = [
'auth' => \App\Http\Middleware\Authenticate::class,
'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
'cache.headers' => \Illuminate\Http\Middleware\SetCacheHeaders::class,
'can' => \Illuminate\Auth\Middleware\Authorize::class,
'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
'password.confirm' => \Illuminate\Auth\Middleware\RequirePassword::class,
'signed' => \Illuminate\Routing\Middleware\ValidateSignature::class,
'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
'verified' => \Illuminate\Auth\Middleware\EnsureEmailIsVerified::class,
'log' => \App\Http\Middleware\AccessLog::class,
];
App\Models\LogRecord.php
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class LogRecord extends Model
{
use HasFactory;
protected $table = 'access_logs';
protected $fillable =['user_id','servicer_id','type','content'];
}
数据库: access_logs table
每次记录保存两次。可能是什么问题?
从您的数据库屏幕截图来看,似乎在几秒钟后添加了重复记录,即。 2-3 秒。所以任何其他请求即。 post 或 ajax 在调用页面后处理。所以请尝试下面的中间件代码 App\Http\Middleware\AccessLog.php
<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Http\Request;
use App\Models\LogRecord;
use Auth;
class AccessLog
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle(Request $request, Closure $next)
{
if($request->method()=='GET')
{
if(strpos($request->path(), 'shop') !== false){
$data['type'] = 'shop';// 'shop' or 'coupon' and for other type 'page' )
$data['content'] = $request->route('id');//content -> shop.id or coupon.id. and other page is route path
}
elseif(strpos($request->path(), 'coupon') !== false){
$data['type'] = 'coupon';
$data['content'] = $request->route('id');
}
else{
$data['type'] = 'page';
$url_components = parse_url($request->path());
$path = explode('/',$url_components['path']);
$data['content'] = $path[0];
}
$user = null;
if (Auth::check()){
$user = Auth::user()->id;
}
$data['user_id'] = $user;
$data['servicer_id'] = config('const.common.accesslog.servicer_id');
$accesslog = LogRecord::create($data);
}
return $next($request);
}
}
谢谢https://whosebug.com/users/16174785/nitrin0 我从 nitrin0 了解到,有时 plugin/extension 可能会影响。所以我试着一个一个地禁用我的每个扩展。在我从我的 google chrome 浏览器中禁用以下扩展 HTML 标签检测器|HTML TAG CHECKER 后它起作用了。太出乎意料了!! extension