当方法为 POST、PUT、DELETE 时,Slim Framework 错误 405
Slim Framework error 405 when methods are POST, PUT, DELETE
我正在开发 SLIM API,使用 slimframework 版本 3.0 RC2。
我已经按照建议配置了 .htaccess 并定义了端点。通过 GET 方法获取时,我得到了正确的 200 响应并且请求的数据已正确部署。
然而,当使用任何 POST、PUT 或 DELETE 方法时,只会收到 405 错误消息,其中指出:
If your Slim Framework application has a route that matches the
current HTTP request URI but NOT the HTTP request method, the
application invokes its Not Allowed handler and returns a HTTP/1.1 405
Not Allowed response to the HTTP client.
这是我正在测试的 POST 端点:
$app->post('/add', function () {
echo "post ok";
});
正如我之前提到的,使用 GET 时,即使使用参数也能正常工作。
我的服务器是 linux debian jessie,PHP 版本 5.6.15。实际上我正在浏览器上测试 URI 并使用 Postman。 POST、PUT DELETE.
中的任何一个都给我一个 405 错误
需要具体使用post方法,或者根据需要进行put、delete。这是来自文档。
$app = new \Slim\App();
$app->post('/books', function ($request, $response, $args) {
// Create new book
});
您不能在 post 请求上使用 $app->get。
编辑:
同时仔细检查您的 htaccess 文件并确保其包含:
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} !-f
RewriteRule ^ index.php [QSA,L]
我找到路了。而不是像这样使用 post:
$app->post('/add', function () {
echo "post ok";
});
使用Js 地图:
$app->map(['GET', 'POST']'/add', function () {
echo "post ok";
});
替换任何其他 PUT、DELETE、PATCH 的 POST 方法......我已经使用数据库进行了测试并且按预期工作正常。
另请记住,您可能或应该需要函数参数:
function($request, $response, $args)
我正在开发 SLIM API,使用 slimframework 版本 3.0 RC2。
我已经按照建议配置了 .htaccess 并定义了端点。通过 GET 方法获取时,我得到了正确的 200 响应并且请求的数据已正确部署。
然而,当使用任何 POST、PUT 或 DELETE 方法时,只会收到 405 错误消息,其中指出:
If your Slim Framework application has a route that matches the current HTTP request URI but NOT the HTTP request method, the application invokes its Not Allowed handler and returns a HTTP/1.1 405 Not Allowed response to the HTTP client.
这是我正在测试的 POST 端点:
$app->post('/add', function () {
echo "post ok";
});
正如我之前提到的,使用 GET 时,即使使用参数也能正常工作。
我的服务器是 linux debian jessie,PHP 版本 5.6.15。实际上我正在浏览器上测试 URI 并使用 Postman。 POST、PUT DELETE.
中的任何一个都给我一个 405 错误需要具体使用post方法,或者根据需要进行put、delete。这是来自文档。
$app = new \Slim\App();
$app->post('/books', function ($request, $response, $args) {
// Create new book
});
您不能在 post 请求上使用 $app->get。
编辑:
同时仔细检查您的 htaccess 文件并确保其包含:
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} !-f
RewriteRule ^ index.php [QSA,L]
我找到路了。而不是像这样使用 post:
$app->post('/add', function () {
echo "post ok";
});
使用Js 地图:
$app->map(['GET', 'POST']'/add', function () {
echo "post ok";
});
替换任何其他 PUT、DELETE、PATCH 的 POST 方法......我已经使用数据库进行了测试并且按预期工作正常。
另请记住,您可能或应该需要函数参数:
function($request, $response, $args)