简体   繁体   English

PHP:发布路线不起作用,找不到404

[英]PHP : Post routes not working, 404 not found

I'm using Slim framework. 我正在使用Slim框架。 I've made an API with Post routes and Get routes 我用发布路线和获取路线制作了一个API
The Get ones are working perfectly 取得成功
The Post ones are not. 邮政的不是。

this one is working when accessed via javascript or php 通过javascript或php访问时,此功能正常工作

$app->get('/test',function(){
 });

While this one return an error 404 not found when accessed 虽然此返回一个错误404,但在访问时未找到

$app->post('/testpost',function(){
 });

I can't figure out the problem 我不明白问题所在
thank you for your help 谢谢您的帮助

Read the docs . 阅读文档

POST Route 开机自检路线

You can add a route that handles only POST HTTP requests with the Slim application's post() method. 您可以使用Slim应用程序的post()方法添加仅处理POST HTTP请求的路由。 It accepts two arguments: 它接受两个参数:

  • The route pattern (with optional named placeholders) 路由模式(带有可选的命名占位符)
  • The route callback 路由回调
$app = new \Slim\App();
$app->post('/books', function ($request, $response, $args) {
    // Create new book
});

If you are posting your data and don't see it, that's because you're not passing any $request parameter to the callback. 如果您发布的数据没有看到,那是因为您没有将任何$request参数传递给回调。

The Slim's router is based on nikic/FastRoute , so if you prefer you may also refer to its docs to better understand it. Slim的路由器基于nikic / FastRoute ,因此,如果您愿意,也可以参考其文档以更好地了解它。

How are you testing? 您如何测试?

Start up the PHP built in web server via php -S 通过php -S启动内置在Web服务器中的php -S

and then I recommend using Curl: 然后我建议使用Curl:

curl -v -X POST http://localhost:8080/testform

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM