由于请求 uri 和请求方法不匹配,Slim 3 出错

Error in Slim 3 because of mismatch in request uri & request method

我正在使用 Slim 3 Framework 作为我的后端和一个小型的自写前端 (jQuery)。在我的前端,我有 ajax 个命令来调用我的 REST 服务器。 现在我面临无法在我的客户端上使用 DELETE 的问题,因为它与 HTTP 请求方法不匹配 (GET).

405 Method not allowed. Must be one of: GET, PUT

官方文档说默认不允许:

If your Slim Framework application has a route that matches the current HTTP request URI but NOT the HTTP request method, the application invokes its Not Allowed handler and returns a HTTP/1.1 405 Not Allowed response to the HTTP client.

现在我可以使用 GETPUT 但这不可能,因为我已经为其他操作声明了这些路由。

Slim Application Error: The application could not run because of the following error: Details Type: FastRoute\BadRouteException Message: Static route /api/v1/folders/ is shadowed by previously defined variable route /api/v1/folders/(.*) for method GET

// Folder routes
$this->group('/folders', function () {
    $this->get('[/{params:.*}]', 'FolderController:index');
    $this->post('', 'FolderController:create');
    $this->put('[/{params:.*}]', 'FolderController:update');
    $this->delete('/[/{params:.*}]', 'FolderController:delete');
})->add('AuthenticateMiddleware');

你能给我一个解决这个问题的建议吗?这不是 REST 世界中的普遍问题吗,因为我猜很多框架都像 Slim 3 一样,在你想使用 DELETE 但可以的特定情况下抛出 405 Method not allowed 错误不是因为浏览器中的点击是GET吗?

根据我的评论:

Is the failing request happening when you click on a link? <a></a> ? The request method has to be DELETE in order for Slim to invoke the right controller. Also note that your delete route has an extra [

祝你好运!