如何在一个路由中使用 PUT 和 PATCH 请求创建另一个更新函数



我目前正在开发一个使用 laravel 5.0 的项目。假设我的路线中有这样的路线.php:

Route::resource('user', 'UserController', ['except' => ['index', 'create', 'store', 'destroy']]);
Route::get('user/{user}/posts', 'UserController@indexUserPosts');
Route::get('user/{user}/changepassword', 'UserController@changePassword');
Route::put('user/{user}/changepassword', 'UserController@updatePassword');
Route::patch('user/{user}/changepassword', 'UserController@updatePassword');

如果我访问http:// localhost:8000/user/{username}它将触发 show 方法,如果我访问http://localhost:8000/user/{username}/edit它将触发编辑方法,该方法将向http:// localhost:8000/user/{user}发出PUTPATCH请求。但是在这个阶段,用户只能编辑他们的个人信息,对于密码,我想创建一个新的editPassword,这也给出了PUT&PATCH请求。我不确定我在上面是否正确编写了路线。

那么,问题是我如何根据 laravel 的约定在 route.php 文件中手动写入路由?

我应该再次向http: //localhost:8000/user/{user}发送 PUT & PATCH 请求(我认为这会随着编辑函数的 PUT & PATCH 请求而崩溃(,还是应该将 PUT & PATCH 请求发送到 http://localhost:8000/user/{user}/changepassword

提前谢谢。 :)

首先,

您不需要重复patch函数。使用put就足够了.第二件事是,每当您创建任何额外的 url 和用户资源时,都需要将它们放在资源路由之前,因此您的路由文件应如下所示:

Route::get('user/{user}/posts', 'UserController@indexUserPosts');
Route::get('user/{user}/changepassword', 'UserController@changePassword');
Route::put('user/{user}/changepassword', 'UserController@update');
Route::resource('user', 'UserController', ['except' => ['index', 'create', 'store', 'destroy']]);
所以现在,当用户转到编辑页面时,

他们将拥有编辑密码页面的链接,当他们单击它时,他们将转到 GET user/{user}/changepassword,当他们填写表单并单击更新时,他们将转到PUT user/{user}/changepassword'

最新更新