Update/Put 路线 - 不允许的方法

Update/Put Route - Method not allowed

我正在使用 postman 来测试我的 API,但是现在我的 put 路由有问题。 所以这是我写的放置路径:

$app->put('/setting/{id}/settingvalue', function (ServerRequestInterface $request, Response

Interface $response, $args) {

    try {
        $user = new \Riecken\PBS\controller\SettingsValueController();
        $result = $user->updateOneSettingsValueinDetail( $args['id'], $request->getParsedBody());
        $response = $response->withJson($result);
        $response = $response->withStatus(200);
        return $response;

    }catch(Exception $e) {

        $response->getBody()->write($e->getMessage());
        return $response->withStatus($e->getCode());

    }

});

这是您在上面看到的函数 (updateOneSettingsValueinDetail):

public function updateOneSettingsValueinDetail ($settingsvalueIdToUpdate, $body) {

    try {

        return $this->SettingsValueDao->update($settingsvalueIdToUpdate, $body);

    }catch(DAOException $e) {
        throw new \Exception($e->returnErrorMessage(), $e->returnHttpCode());
    }catch(\Exception $e) {
        throw new \Exception("System Error", 500);
    }


}

问题是 Postman 告诉我不允许使用 Method,只允许 POST 和 GET: enter image description here

有人知道这是什么类型的问题以及解决方案吗?

此响应来自 Slim 的 NotAllowedHandler。而且它不仅是 POST 和默认的 GET。此回复与您上述代码无关。

您确定不自定义 "NotAllowedHandler" 并且不作为中间件绑定到应用程序吗?

我写了这段代码,包含它会造成同样的情况:

<?php

use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Slim\App;

require __DIR__ . '/../vendor/autoload.php';

$app = new App([]);

$container = $app->getContainer();

$app->add(function ($request, $response, $next) {
    $allowed = ['GET', 'POST'];
    if (!in_array($request->getMethod(), $allowed)) {
        $notAllowed = new \Slim\Handlers\NotAllowed();
        return $notAllowed($request, $response, $allowed);
    }
    $next($request, $response);
});

$app->put('/setting/{id}/settingvalue', function (ServerRequestInterface $request, ResponseInterface $response, $args) {
    die("Expected Context via PUT");
});

$app->get('/setting/{id}/settingvalue', function (ServerRequestInterface $request, ResponseInterface $response, $args) {
    die("Expected Other Context via GET");
});

$app->run();

希望对你有所帮助