Slim 框架和 GET/PUT/POST 方法

编程入门 行业动态 更新时间:2024-10-24 18:18:23
本文介绍了Slim 框架和 GET/PUT/POST 方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送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:关键词]

本文发布于:2023-04-18 16:24:13,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/942746.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:框架   方法   Slim   POST   PUT

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!