Laravel route method DELETE not working

耗尽温柔 提交于 2021-02-11 06:52:09

问题


In my routes.php, when I have:

    Route::delete('page/{id}', function ($id)
    {
        return "deleting $id";
    });

And I send a delete or get request using Postman, This throws a MethodNotAllowedHttpException.

When I change routes.php:

    Route::get('page/{id}', function ($id)
    {
        return "deleting $id";
    });

It responds the string deleting... in response to GET, DELETE and PUT! But the HTTP code is 403.

It just throws a MethodNotAllowedHttpException on a POST request.

This problem seems to occur only on remote server and it works as expected on localhost.

Is there anything in Laravel that maybe redirects or changes methods to GET?


回答1:


It's because Apache doesn't allow DELETE requests, and that's why the response code is a "403 forbidden".

Add this to .htaccess after the Laravel default codes:

<Limit DELETE>
  Order deny,allow
  Allow from all
</Limit>

see this answer: https://stackoverflow.com/a/1402480/2543240




回答2:


Try to add this to your form, above delete button:

{!! method_field('DELETE') !!}
<input type="hidden" name="_method" value="DELETE">


来源:https://stackoverflow.com/questions/36531124/laravel-route-method-delete-not-working

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!