laravel / lumen-framework

The Laravel Lumen Framework.
https://lumen.laravel.com
MIT License
1.47k stars 419 forks source link

Multiple methods (GET and POST) for same route not working #1236

Closed RBFraphael closed 2 years ago

RBFraphael commented 2 years ago

Description:

I'm developing a microsservice with Lumen, and I need to create a root route that acceps GET and POST methods. Tried:

  1. Two declarations with same path
    $route->get("/", "ApiModule@list");
    $route->post("/", "ApiModule@create");
  2. One declaration, with multiple methods, pointing to a function that checks method then calls right function
    $route->addRoute(["GET", "POST"], "/", "ApiModule@get_post_handler");
    public function get_post_handler(Request $request)
    {
    if($request->isMethod("GET")){
        return $this->list($request);
    } else if($request->isMethod("POST")){
        return $this->create($request);
    } else {
        return response(null, 405);
    }
    }

    When calling POST localhost/test/api, it detects the request as a GET, never calling the create function. But, when trying with a trailing slash when requesting POST (for example, POST localhost/test/api/ instead POST localhost/test/api), GET requests not works anymore, and only POST is working (he never calls the list function).

Steps To Reproduce:

  1. Declare two routes with same path/URI and different methods and actions
  2. Try to call them within Postman, either GET and POST, with and without a trailing slash
driesvints commented 2 years ago

Hi there,

Thanks for reporting but it looks like this is a question which can be asked on a support channel. Please only use this issue tracker for reporting bugs with the library itself. If you have a question on how to use functionality provided by this repo you can try one of the following channels:

However, this issue will not be locked and everyone is still free to discuss solutions to your problem!

Thanks.

RBFraphael commented 2 years ago

@driesvints I'm sorry for the persistence, but this looks like a bug for me, because only on root routes, I can't add a POST route (like having two root routes, one POST / and one GET /). This is weird. I've tried to POST to another route just for testing, and tried creating another two routes with same URI and multiple methods (for example, POST /test and GET /test), and it's working fine. Only in root URI I can't create POST and GET routes.

I think this may be a bug because, like in my scenario, developers can't make forms or microsservices that POST something to root (like a landing page with a form, or a microsservice which his base URL is managed at server).

driesvints commented 2 years ago

Lumen uses fast route behind the scenes so you need to post an issue on their repo. We don't manage this.