I'm trying to get current url in laravel without any parameters or query parameters, let's assume I've this route in the api.php file:
test/{id}/get-test-data/{type}
and the url is
test/395/get-test-data/temp
I need to log this basic url in the middle-ware test/{id}/get-test-data/{type}
and not the one with parameters,
I've tried this
$route = \Route::current();
$route->uri;
it worked, but when the endpoint being hit the middle-ware print the $route->uri twice as the following
test/395/get-test-data/temp
test/{id}/get-test-data/{type}
is there any way to avoid printing the first line?
CodePudding user response:
according to this answer 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.
So the solution is to filter out these option requests in the middleware:
if ($request->method() != 'OPTIONS') {
...
}