我的api遵循源路径Src/Api/Notes/index.php:
<?php
if ($_SERVER['REQUEST_METHOD'] === 'GET') {
// TODO
} elseif ($_SERVER['REQUEST_METHOD'] === 'POST') {
// TODO
} elseif ($_SERVER['REQUEST_METHOD'] === 'PUT') {
// TODO
} elseif ($_SERVER['REQUEST_METHOD'] === 'DELETE') {
// TODO
} else {
// TODO echo error
}
我使用的URL是localhost/api/notes,当我使用HTTP GET时,它会正常发生。但是当我使用Post、Put、delete时,收到的$_SERVER['REQUEST_METHOD']总是Get。
这是我的.htaccess 文件:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
RewriteRule ^api/(.*)$ src/api/$1 [QSA,L]
帮帮我,谢谢
我尝试更改除index.php名称之外的文件名,然后http方法可以正常工作。 谢谢你
这可能是由于Apache中的目录斜杠而发生的。默认情况下,Apache 中的
DirectorySlash
是 ON
,它将把不带文件扩展名的请求视为目录请求。
示例: 如果您发出请求
localhost/api/notes
,Apache 将附加斜杠 localhost/api/notes/
并向客户端发出重定向。即使原始请求不是 GET
请求,这也会向 localhost/api/notes/
发出 GET
请求。要解决此问题,您必须关闭 DirectorySlash
中的
.htaccess
RewriteEngine On
DirectorySlash Off
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
RewriteRule ^api/(.*)$ src/api/$1 [QSA,L]