Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

RBFraphael's avatar

Can't create a POST route pointing to application's root (/)

I'm creating some microsservices with Lumen, which will have each base url managed by a server, and I need to create a POST route at application's root.

This way, the server (Apache, NGINX, etc) will point api.domain.com/users, api.domain.com/courses, api.domain.com/teachers etc to each application, so creating and listing entries will be executed at root. But, when I try to POST something to root route, it's returning a GET request instead a POST.

My web.php routing file looks this way:

$router->get("/", "ApiModule@list");
$router->get("/{id}", "ApiModule@read");
$router->post("/", "ApiModule@create");
$router->delete("/{id}", "ApiModule@delete");
$router->put("/{id}", "ApiModule@update");

Testing GET requests with Postman and Insomnia to /, I receive the expected result: a JSON formatted list of registered courses. But, when I try to create a new course sending a POST request to /, it's not working as expected, since is returning a JSON formatted list of registered courses (like the GET route) and doesn't create a new course.

Testing with $route->addRoute(["GET", "POST", "PUT", "DELETE"], "/", "ApiModule@test") to create a multi-method route that points to a single function in my controller, and "dding" $request->method() in that function, it's always returning GET, even with POST, PUT and DELETE requests. This doesn't happens with other routes than root.

0 likes
5 replies
RBFraphael's avatar

I'm sorry, but what you mean saying "your last question" ? I've only asked about the impossibility of creating a POST route pointing to application's root uri (/)...

The behaviour I'm facing is this: I have two routes pointing to root uri, one GET and other POST. The GET works as expected, but the POST is working as the GET. Maybe there is a redirect or something else. And, if I remove/comment out the GET route declaration, when I call POST, I receive a 405 Method Not Allowed error.

1 like
vincent15000's avatar

@RBFraphael Here is your last question.

Testing with $route->addRoute(["GET", "POST", "PUT", "DELETE"], "/", "ApiModule@test") to create a multi-method route that points to a single function in my controller, and "dding" $request->method() in that function, it's always returning GET, even with POST, PUT and DELETE requests. This doesn't happens with other routes than root.

RBFraphael's avatar

After questioned by a friend, tried to test requests with a trailing slash on URI, and, for my surprise, it's worked. But, now, I want to understand why this is needed and how to fix that, because it's a bit tricky to tell developers who will integrate this microsservices in front-end they need to add a trailing slash all times they make a request, and, maybe, some front-end frameworks may strip these slashes from URL when making AJAX requests.

1 like
Snapey's avatar

i think it's to do with your .htaccess file and rewriting

I would simply add something to the route eg

$router->post("/data", "ApiModule@create");
1 like

Please or to participate in this conversation.