简体   繁体   中英

PHP : Post routes not working, 404 not found

I'm using Slim framework. I've made an API with Post routes and Get routes
The Get ones are working perfectly
The Post ones are not.

this one is working when accessed via javascript or php

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

While this one return an error 404 not found when accessed

$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. 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.

The Slim's router is based on nikic/FastRoute , so if you prefer you may also refer to its docs to better understand it.

How are you testing?

Start up the PHP built in web server via php -S

and then I recommend using Curl:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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