admin管理员组

文章数量:1130349

I am working on an API for a Laravel 11 project, and I'm facing an issue where PUT, PATCH, and DELETE requests are not being supported, and I keep getting the following error:

Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException
PHP 8.2.24
Laravel 11.19.0
The PUT method is not supported for route /. Supported methods: GET, HEAD.

However, GET and POST requests are working perfectly fine. I have checked the server configuration, and the PUT, PATCH, and DELETE requests are reaching the server correctly. Laravel is correctly detecting the request method, but I am still getting a 405 Method Not Allowed error.

The routes are also registered correctly, as shown below:

GET|HEAD        api/v2/categories ........................... categories.index › Api\CategoryController@index
POST            api/v2/categories ........................... categories.store › Api\CategoryController@store
GET|HEAD        api/v2/categories/{category} .................. categories.show › Api\CategoryController@show
PUT|PATCH       api/v2/categories/{category} .............. categories.update › Api\CategoryController@update
DELETE          api/v2/categories/{category} ............ categories.destroy › Api\CategoryController@destroy
GET|HEAD        dashboard/categories ............... dashboard.categories › App\Livewire\Dashboard\Categories
GET|HEAD        dashboard/categories/{category}/edit .. categories.edit › App\Livewire\Dashboard\EditCategory

Here's the route code I'm using:

Route::middleware([ApiTokenMiddleware::class])
    ->prefix('v2')
    ->group(function () {

        Route::put('/categories/{category}', [CategoryController::class, 'update']);
    });

What I have tried:

  • Server Configuration: I have checked and confirmed the server (Apache/Nginx) configuration supports PUT and DELETE methods.
  • Laravel Routes: The routes are correctly registered in the output of php artisan route:list.
  • Request Method: I verified that the requests are correctly reaching the server with the expected methods (PUT, PATCH, DELETE).

Still, I am receiving a 405 Method Not Allowed error. Can anyone help me identify why this is happening and how to resolve it?

# UPDATE 1

Guys, this is some kind of anomaly! I've already configured nginx, changed apache settings, and checked .htaccess settings. Nothing worked. I decided to go with trial and error, and eventually realized that PUT requests go through fine. But what’s still unclear to me is why they are being redirected—I haven't figured that out yet. The problem is definitely not with the server, I’ve checked that multiple times. The issue is in my code block. Requests are passing through the middleware fine, but then something weird happens. Just to mention, Laravel has its own middleware that handles all this.

So, I blindly wrote:

Route::put('api/v2/categories/{id}', function($id) {
    echo 'He-He!' . $id;
});

I disabled CSRF protection first, and this thing worked, without redirects and all that.

I am working on an API for a Laravel 11 project, and I'm facing an issue where PUT, PATCH, and DELETE requests are not being supported, and I keep getting the following error:

Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException
PHP 8.2.24
Laravel 11.19.0
The PUT method is not supported for route /. Supported methods: GET, HEAD.

However, GET and POST requests are working perfectly fine. I have checked the server configuration, and the PUT, PATCH, and DELETE requests are reaching the server correctly. Laravel is correctly detecting the request method, but I am still getting a 405 Method Not Allowed error.

The routes are also registered correctly, as shown below:

GET|HEAD        api/v2/categories ........................... categories.index › Api\CategoryController@index
POST            api/v2/categories ........................... categories.store › Api\CategoryController@store
GET|HEAD        api/v2/categories/{category} .................. categories.show › Api\CategoryController@show
PUT|PATCH       api/v2/categories/{category} .............. categories.update › Api\CategoryController@update
DELETE          api/v2/categories/{category} ............ categories.destroy › Api\CategoryController@destroy
GET|HEAD        dashboard/categories ............... dashboard.categories › App\Livewire\Dashboard\Categories
GET|HEAD        dashboard/categories/{category}/edit .. categories.edit › App\Livewire\Dashboard\EditCategory

Here's the route code I'm using:

Route::middleware([ApiTokenMiddleware::class])
    ->prefix('v2')
    ->group(function () {

        Route::put('/categories/{category}', [CategoryController::class, 'update']);
    });

What I have tried:

  • Server Configuration: I have checked and confirmed the server (Apache/Nginx) configuration supports PUT and DELETE methods.
  • Laravel Routes: The routes are correctly registered in the output of php artisan route:list.
  • Request Method: I verified that the requests are correctly reaching the server with the expected methods (PUT, PATCH, DELETE).

Still, I am receiving a 405 Method Not Allowed error. Can anyone help me identify why this is happening and how to resolve it?

# UPDATE 1

Guys, this is some kind of anomaly! I've already configured nginx, changed apache settings, and checked .htaccess settings. Nothing worked. I decided to go with trial and error, and eventually realized that PUT requests go through fine. But what’s still unclear to me is why they are being redirected—I haven't figured that out yet. The problem is definitely not with the server, I’ve checked that multiple times. The issue is in my code block. Requests are passing through the middleware fine, but then something weird happens. Just to mention, Laravel has its own middleware that handles all this.

So, I blindly wrote:

Route::put('api/v2/categories/{id}', function($id) {
    echo 'He-He!' . $id;
});

I disabled CSRF protection first, and this thing worked, without redirects and all that.

本文标签: