【问题标题】:Slim framework and GET/PUT/POST methodsSlim 框架和 GET/PUT/POST 方法
【发布时间】:2015-02-08 15:25:55
【问题描述】:

例如,我使用此代码来测试路线:

$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 请求时,(在响应标头中:允许:GET、HEAD、POST、OPTIONS、TRACE)Slim 响应有 405 错误(不允许方法)并显示消息:

“请求的方法 PUT 不允许用于 URL /api。”

我做错了什么?

【问题讨论】:

  • 它适用于我使用 Advanced REST clientDHC - REST/HTTP API Client。你添加.htaccess文件了吗?
  • 是的,这是我的 htaccess: RewriteEngine On RewriteBase / RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)/$ index.php
  • 尝试使用this one
  • 感谢我的朋友,辛苦了!
  • 很高兴看到它有效。出于订购目的,我已经用解决方案创建了一个答案。

标签: api methods httprequest slim


【解决方案1】:

确保您的.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]

【讨论】:

  • 感谢您发布此信息。 Davide Pastore 的链接对我不起作用,这就是我在拔掉头发两天后最终帮助我一切正常的原因。非常感谢。
  • @ramijames 感谢您的评论。我更新了我的答案,修复了损坏的链接和 .htaccess 内容。
猜你喜欢
  • 2013-06-12
  • 1970-01-01
  • 1970-01-01
  • 2023-03-09
  • 1970-01-01
  • 2016-06-01
  • 1970-01-01
  • 1970-01-01
  • 2015-12-16
相关资源
最近更新 更多