问题描述
限时送ChatGPT账号..例如,我使用此代码来测试路由:
$app->get('/api', function () {echo 'get!';});$app->post('/api', function () {echo '发布!';});$app->put('/api', function () {echo '放!';});
对于 api 测试,我使用了 Chrome 的 RestClient 插件.
当我尝试执行 GET 请求时,响应是get!".挺好的.
但是:
当我尝试执行 POST 请求时,响应也是get!".为什么?它必须是发布!".
当我尝试执行 PUT 请求时,(在响应标头中:允许:GET,HEAD,POST,OPTIONS,TRACE)Slim 响应有 405 错误(方法不允许),并带有消息:
<块引用>
URL/api 不允许请求的方法 PUT."
我做错了什么?
解决方案请确保您的 .htaccess
如下(来自 slimphp/Slim@2.x):
RewriteEngine On# 一些主机可能要求你使用 `RewriteBase` 指令.# 如果你需要使用 `RewriteBase` 指令,它应该是# 包含此 htaccess 文件的目录的绝对物理路径.## 重写基数/RewriteCond %{REQUEST_FILENAME} !-dRewriteCond %{REQUEST_FILENAME} !-f重写规则 ^ index.php [QSA,L]
For example, i use this code for testing routes:
$app->get('/api', function () {
echo 'get!';
});
$app->post('/api', function () {
echo 'post!';
});
$app->put('/api', function () {
echo 'put!';
});
For api testing i use RestClient plugin for Chrome.
When i try do GET request, response is 'get!'. Its good.
But:
When i try do POST request, response also is 'get!'. Why? Its must be 'post!'.
When i try do PUT request, (in Response Headers: Allow: GET,HEAD,POST,OPTIONS,TRACE ) Slim response have 405 error (Method Not Allowed) with message:
"The requested method PUT is not allowed for the URL /api."
What am I doing wrong?
解决方案Be sure that your .htaccess
is the following (from 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]
这篇关于Slim 框架和 GET/PUT/POST 方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
更多推荐
[db:关键词]
发布评论