MethodNotAllowedHttpException

MethodNotAllowedHttpException

本文介绍了Laravel 表单不会 PATCH,只有 POST - 嵌套的 RESTfull 控制器,MethodNotAllowedHttpException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试允许用户编辑他们的播放列表.但是,每当我尝试执行 PATCH 请求时,都会收到 MethodNotAllowedHttpException 错误.(它正在等待一个 POST)

I am trying to allow users to edit their playlist. However, whenever I try to execute the PATCH request, I get the MethodNotAllowedHttpException error. (it is expecting a POST)

我已经设置了 RESTful 资源控制器:

I have set up RESTful Resource Controllers:

Routes.php:

Routes.php:

Route::resource('users', 'UsersController');
Route::resource('users.playlists', 'PlaylistsController');

这应该让我可以访问:(通过 php artisan routes 显示)

This should give me access to: (as displayed through php artisan routes)

URI                                        | Name                   | Action
PATCH users/{users}/playlists/{playlists}  | users.playlists.update | PlaylistsController@update

但是,当我尝试执行以下表单时,我收到 MethodNotAllowedHttpException 错误:

However, when I try to execute the following form, I get the MethodNotAllowedHttpException error:

/users/testuser/playlists/1/edit

/users/testuser/playlists/1/edit

{{ Form::open(['route' => ['users.playlists.update', $playlist->id], 'method' => 'PATCH' ]) }}
{{ Form::text('title', $playlist->title) }}
{{ Form::close() }}

如果我删除 'method'=>'PATCH' 我没有收到错误,但它执行我的 public function store() 而不是我的 public function update()

If I remove 'method'=> 'PATCH' I don't get an error, but it executes my public function store() and not my public function update()

推荐答案

在 Laravel 5 及更高版本中:

In Laravel 5 and up:

<form method="POST" action="patchlink">
    @method('patch')
    . . .
</form>

这篇关于Laravel 表单不会 PATCH,只有 POST - 嵌套的 RESTfull 控制器,MethodNotAllowedHttpException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-26 07:23