Search code examples
phprestrestful-architecturerestful-urlslim-3

Error in Slim 3 because of mismatch in request uri & request method


I'm using Slim 3 Framework as my backend and a small self-written frontend (jQuery). In my frontend I have ajax commands to call my REST server. Now I'm facing the problem that I can't use DELETE on my client because it is not matching the HTTP request method (GET).

405 Method not allowed. Must be one of: GET, PUT

The official documentation says said it is not allowed by default:

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.

Now I could use GET or PUT but that is not possibly because I already have those routes declared for other actions.

Slim Application Error: The application could not run because of the following error: Details Type: FastRoute\BadRouteException Message: Static route /api/v1/folders/ is shadowed by previously defined variable route /api/v1/folders/(.*) for method GET

// Folder routes
$this->group('/folders', function () {
    $this->get('[/{params:.*}]', 'FolderController:index');
    $this->post('', 'FolderController:create');
    $this->put('[/{params:.*}]', 'FolderController:update');
    $this->delete('/[/{params:.*}]', 'FolderController:delete');
})->add('AuthenticateMiddleware');

Could you please give me an advice on how to solve this? Isn't this a general problem in the REST-world so to speak, because I guess many frameworks act like Slim 3 and throw a 405 Method not allowed error in such particular situation where you want to use DELETE but can't because the click in the browser is GET?


Solution

  • As per my comment:

    Is the failing request happening when you click on a link? <a></a> ? The request method has to be DELETE in order for Slim to invoke the right controller. Also note that your delete route has an extra [

    Good luck !