gpt4 book ai didi

api - Slim 框架和 GET/PUT/POST 方法

转载 作者:行者123 更新时间:2023-12-04 02:21:39 25 4
gpt4 key购买 nike

例如,我使用这段代码来测试路由:

$app->get('/api', function () {                         
echo 'get!';
});
$app->post('/api', function () {
echo 'post!';
});
$app->put('/api', function () {
echo 'put!';
});

对于 api 测试,我使用 Chrome 的 RestClient 插件。

当我尝试执行 GET 请求时,响应是“get!”。很好。

但是:

  1. 当我尝试执行 POST 请求时,响应也是“获取!”。为什么?它必须是“发布!”。

  2. 当我尝试执行 PUT 请求时,(在响应 header 中:允许:GET、HEAD、POST、OPTIONS、TRACE)Slim 响应有 405 错误(方法不允许),消息为:

"The requested method PUT is not allowed for the URL /api."

我做错了什么?

最佳答案

确保您的 .htaccess 是以下内容(来自 slimphp/Slim@2.x ):

RewriteEngine On

# Some hosts may require you to use the `RewriteBase` directive.
# If you need to use the `RewriteBase` directive, it should be the
# absolute physical path to the directory that contains this htaccess file.
#
# RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]

关于api - Slim 框架和 GET/PUT/POST 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28395692/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com