Slim 3 Http 删除方法不起作用

Slim 3 Http Delete method not working

我正在尝试实施 http DELETE。该应用程序使用 php 和 slim3 框架编写。前端是 angular 2.

如果模式看起来像:$slimApp->delete('/delete', ...)一切都很好。

只要我引入如下参数:$slimApp->delete('/delete/{id}', ...) 我就会收到以下错误:

Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.

我已经阅读了文档 https://www.slimframework.com/docs/cookbook/enable-cors.html 但是我无法让它工作。

这是我的中间件:

<?php

class MyMiddleware {

    public function __invoke(Request $req, Response $res, $next) {
        $res->withHeader('Access-Control-Allow-Origin', 'http://localhost:8100')
            ->withHeader('Access-Control-Allow-Credentials', 'true')
            ->withHeader('Access-Control-Allow-Headers', 'X-Requested-With, Content-Type, Accept, Origin, Authorization')
            ->withHeader('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS');

        return $next($req, $res);
    }

}

$app = new \Slim\App([
    "settings"  => [
        "determineRouteBeforeAppMiddleware" => true
    ]
]);

$app->add(new MyMiddleware());

任何想法可能是什么问题?

响应是不可变的,重新分配变量。

public function __invoke(Request $req, Response $res, $next) {
    $res = $res->withHeader('Access-Control-Allow-Origin', 'http://localhost:8100')
        ->withHeader('Access-Control-Allow-Credentials', 'true')
        ->withHeader('Access-Control-Allow-Headers', 'X-Requested-With, Content-Type, Accept, Origin, Authorization')
        ->withHeader('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS');

    return $next($req, $res);
}

好的,问题解决了。 .htaccess 只是没有让请求"reach" 的代码。所以我只需要优化规则。