Laravel 中间件在路由中执行两次参数
我有这个非常简单的中间件
namespace App\Http\Middleware;
use Closure;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
class CacheResponseMinify
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle(Request $request, Closure $next)
{
Log::debug('***********************************************************');
Log::debug($request);
return $next($request);
}
}
这个中间件与一个路由和一个参数相关联:
Route::get('/route/{page}', [App\Http\Controllers\MyController::class, 'page'])->middleware([ 'cacheResponseMinify']);
我在浏览器中输入这个 URL:http://localhost:81/route/my_page
在日志中,hte 中间件的第一次执行显示一个空数组:
[2022-03-01 14:38:48] local.DEBUG: array (
)
第二次执行显示一个数组,其中包含来自加载的 URL 的数据。
[2022-03-01 14:38:51] local.DEBUG: array (
'action_name' => 'Title_of_the_page',
'idsite' => '0',
'rec' => '1',
'r' => '588131',
'h' => '14',
'm' => '38',
's' => '50',
'url' => 'http://localhost:81/route/my_page',
'_id' => '746a68055b9493f2',
'_idts' => '1646141931',
'_idvc' => '1',
'_idn' => '1',
'_refts' => '0',
'_viewts' => '1646141931',
'send_image' => '1',
'pdf' => '1',
'qt' => '0',
'realp' => '0',
'wma' => '0',
'dir' => '0',
'fla' => '0',
'java' => '0',
'gears' => '0',
'ag' => '0',
'cookie' => '1',
'res' => '1920x1080',
'gt_ms' => '404',
'pv_id' => 'iGGVy6',
)
如果路由是绝对的,没有参数,则
Route::get('/route/my_page', [App\Http\Controllers\MyController::class, 'page'])->middleware([ 'cacheResponseMinify']);
中间件仅执行一次。
您知道为什么使用路由和参数,中间件会执行两次吗?
I have this very simple middleware
namespace App\Http\Middleware;
use Closure;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
class CacheResponseMinify
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle(Request $request, Closure $next)
{
Log::debug('***********************************************************');
Log::debug($request);
return $next($request);
}
}
This middleware is associated with a route and a parameter :
Route::get('/route/{page}', [App\Http\Controllers\MyController::class, 'page'])->middleware([ 'cacheResponseMinify']);
I enter this URL in browser : http://localhost:81/route/my_page
In the log, the first execution of hte middleware shows an empty array :
[2022-03-01 14:38:48] local.DEBUG: array (
)
The second execution shows an array with datas from the loaded URL
[2022-03-01 14:38:51] local.DEBUG: array (
'action_name' => 'Title_of_the_page',
'idsite' => '0',
'rec' => '1',
'r' => '588131',
'h' => '14',
'm' => '38',
's' => '50',
'url' => 'http://localhost:81/route/my_page',
'_id' => '746a68055b9493f2',
'_idts' => '1646141931',
'_idvc' => '1',
'_idn' => '1',
'_refts' => '0',
'_viewts' => '1646141931',
'send_image' => '1',
'pdf' => '1',
'qt' => '0',
'realp' => '0',
'wma' => '0',
'dir' => '0',
'fla' => '0',
'java' => '0',
'gears' => '0',
'ag' => '0',
'cookie' => '1',
'res' => '1920x1080',
'gt_ms' => '404',
'pv_id' => 'iGGVy6',
)
If the route is absolute, wihout parameter
Route::get('/route/my_page', [App\Http\Controllers\MyController::class, 'page'])->middleware([ 'cacheResponseMinify']);
the middleware is executed only once.
Have you some idea why with a route and parameter, the middleware is executed twice ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我最终找到了问题的根源。
对于每个页面,我都使用 Matomo 设置统计监控。
有一个脚本 matomo.js 会触发对 matomo.php?list_of_parameters 的调用。
通过查阅 http://localhost:81/route/my_page,matomo 脚本调用 http://localhost:81/route/matomo.php?list_of_parameters
路由中,有:
路线::get('/route/{page}'
http://localhost:81/route/my_page
和
http://localhost:81/route/matomo.php?list_of_parameters
两者都经过这条路线。
在控制器中,my_page对应的是具体的视图,OK,缓存正确。
另一方面,matomo.php?list_of_parameters 进入默认视图,即
Laravel 目录的主页,因此缓存的行为完全正常:-)
错误在其他地方。
matomo.js 文件使用 matomo_appli.js 中定义的参数。
在文件 matomo_appli.js 中定义了 URL_MATOMO。
对于本地环境和开发环境,它是一个空字符串。
这就是为什么调用的统计 url 是 http://localhost:81/route/matomo.php?list_of_parameters
我修改为将 URL_MATOMO 与指向 http://localhost:81/matomo/ 的链接放在一起
现在,matomo 统计链接是形式 http://localhost:81/matomo/matomo.php?list_of_parameters
因此,不再需要经历 Laravel 路由或计划外缓存。
I ended up finding the source of the problem.
For each page, i set up statistical monitoring with Matomo.
There is a script matomo.js which triggers the call to matomo.php?list_of_parameters.
By consulting http://localhost:81/route/my_page, the matomo script calls http://localhost:81/route/matomo.php?list_of_parameters
In the route, there are:
Route::get('/route/{page}'
http://localhost:81/route/my_page
and
http://localhost:81/route/matomo.php?list_of_parameters
both pass by this route.
In the controller, my_page corresponds to a specific view, OK, caching is correct.
On the other hand, matomo.php?list_of_parameters goes on the default view which is the home page of the directory
Laravel and caching therefore behave completely normally :-)
The error was elsewhere.
The matomo.js file uses parameters defined in matomo_appli.js.
In the files matomo_appli.js is defined URL_MATOMO.
For local environnment and dev environnment, it was an empty string.
That's why the stats url called was http://localhost:81/route/matomo.php?list_of_parameters
I modified to put URL_MATOMO with a link pointing to http://localhost:81/matomo/
Now, the matomo stat links are of the form http://localhost:81/matomo/matomo.php?list_of_parameters
And so, no more going through a Laravel route or unplanned caching.