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.
本文标签:
版权声明:本文标题:php - Laravel 11 PUT, PATCH, DELETE requests returning 405 Method Not Allowed, but GET and POST work fine - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/questions/1736707632a1431292.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


发表评论