Laravel 5.8: 如何在中间件中不带查询或参数的情况下获取匹配的URL路径?

Laravel 5.8: How to get the matched URL path without queries or parameters in the middle ware?

我正在尝试在 laravel 中获取当前的 url,而不需要任何参数或查询参数, 假设我在 api.php 文件中有这条路线:

test/{id}/get-test-data/{type}

而 url 是

test/395/get-test-data/temp

我需要在中间件 test/{id}/get-test-data/{type} 中记录这个基本 url 而不是带有参数的那个,

我试过了

$route = \Route::current();
$route->uri;

它起作用了,但是当端点被命中时,中间件打印 $route->uri 两次,如下所示

test/395/get-test-data/temp
test/{id}/get-test-data/{type}

有什么办法可以避免打印第一行吗?

根据这个回答here

When you send a POST, PUT, DELETE or PATCH request to /api/posts, the browser sends an OPTIONS request first, so laravel "registers" all the routes and then it executes the "matching" using the URL and the HTTP method (it is OPTIONS right now).

But there is no route that has a method of OPTIONS and resources don't have an OPTIONS method either, so since there is no route that has an OPTIONS method, laravel doesn't match anything and therefore it does not execute those middlewares where you eventually handle OPTIONS methods.

所以解决办法就是在中间件中过滤掉这些选项请求:

if ($request->method() != 'OPTIONS') {
        ...
}