Slim
Slim copied to clipboard
When route not found it returns 405 method not allowed
When try to send request to wrong route its return
{
"error": "Method not allowed. Must be one of: OPTIONS",
"details": {
"type": "NOT_ALLOWED",
"exception": "Slim\\Exception\\HttpMethodNotAllowedException",
"message": "Method not allowed. Must be one of: OPTIONS",
"code": 405,
"file": "\/opt\/vhost\/vendor\/slim\/slim\/Slim\/Middleware\/RoutingMiddleware.php",
"line": 94,
"trace": [
"#0 \/opt\/vhost\/vendor\/slim\/slim\/Slim\/Middleware\/RoutingMiddleware.php(58): Slim\\Middleware\\RoutingMiddleware->performRouting(Object(Slim\\Psr7\\Request))",
"#1 \/opt\/vhost\/vendor\/slim\/slim\/Slim\/MiddlewareDispatcher.php(147): Slim\\Middleware\\RoutingMiddleware->process(Object(Slim\\Psr7\\Request), Object(Psr\\Http\\Server\\RequestHandlerInterface@anonymous))",
"#2 \/opt\/vhost\/vendor\/slim\/slim\/Slim\/Middleware\/ErrorMiddleware.php(107): Psr\\Http\\Server\\RequestHandlerInterface@anonymous->handle(Object(Slim\\Psr7\\Request))",
"#3 \/opt\/vhost\/vendor\/slim\/slim\/Slim\/MiddlewareDispatcher.php(147): Slim\\Middleware\\ErrorMiddleware->process(Object(Slim\\Psr7\\Request), Object(Psr\\Http\\Server\\RequestHandlerInterface@anonymous))",
"#4 \/opt\/vhost\/vendor\/slim\/slim\/Slim\/Middleware\/ContentLengthMiddleware.php(27): Psr\\Http\\Server\\RequestHandlerInterface@anonymous->handle(Object(Slim\\Psr7\\Request))",
"#5 \/opt\/vhost\/vendor\/slim\/slim\/Slim\/MiddlewareDispatcher.php(147): Slim\\Middleware\\ContentLengthMiddleware->process(Object(Slim\\Psr7\\Request), Object(Psr\\Http\\Server\\RequestHandlerInterface@anonymous))",
"#6 \/opt\/vhost\/module\/Api\/Middleware\/MethodOverrideMiddleware.php(50): Psr\\Http\\Server\\RequestHandlerInterface@anonymous->handle(Object(Slim\\Psr7\\Request))",
"#7 \/opt\/vhost\/vendor\/slim\/slim\/Slim\/MiddlewareDispatcher.php(147): Api\\Middleware\\MethodOverrideMiddleware->process(Object(Slim\\Psr7\\Request), Object(Psr\\Http\\Server\\RequestHandlerInterface@anonymous))",
"#8 \/opt\/vhost\/vendor\/slim\/slim\/Slim\/Middleware\/BodyParsingMiddleware.php(68): Psr\\Http\\Server\\RequestHandlerInterface@anonymous->handle(Object(Slim\\Psr7\\Request))",
"#9 \/opt\/vhost\/vendor\/slim\/slim\/Slim\/MiddlewareDispatcher.php(147): Slim\\Middleware\\BodyParsingMiddleware->process(Object(Slim\\Psr7\\Request), Object(Psr\\Http\\Server\\RequestHandlerInterface@anonymous))",
"#10 \/opt\/vhost\/vendor\/slim\/slim\/Slim\/MiddlewareDispatcher.php(81): Psr\\Http\\Server\\RequestHandlerInterface@anonymous->handle(Object(Slim\\Psr7\\Request))",
"#11 \/opt\/vhost\/vendor\/slim\/slim\/Slim\/App.php(215): Slim\\MiddlewareDispatcher->handle(Object(Slim\\Psr7\\Request))",
"#12 \/opt\/vhost\/vendor\/slim\/slim\/Slim\/App.php(199): Slim\\App->handle(Object(Slim\\Psr7\\Request))",
"#13 \/opt\/vhost\/httpdocs\/api\/index.php(134): Slim\\App->run()",
"#14 {main}"
],
"previous": null
}
}
But it should return 404 error
When i check Slim code I have found it always check is method allowed always first it should not be. I have added some code in slim source it fixes the issue
In vendor/slim/slim/Slim/Routing/FastRouteDispatcher.php line number 51
if (!empty($this->getAllowedMethods($uri)) && $routingResults[0] !== self::NOT_FOUND ) {
return [self::METHOD_NOT_ALLOWED, null, []];
}
It fixes the issue
Hi @misyaath , thank you for the discussion. The error handler is correctly set up to handle 404 responses, but we need to modify the routing middleware to properly throw HttpNotFoundException for non-existent routes. You can modify the public/index.php of your slim app file to add a custom 404 handler:
$app->map(['GET', 'POST', 'PUT', 'DELETE', 'PATCH'], '/{routes:.+}', function ($request, $response) {
throw new \Slim\Exception\HttpNotFoundException($request);
});