Laravel 5方法未找到异常 | 珊瑚贝

Laravel 5 Method not found exception


我使用 Laravel 5 并尝试更新表单:

1
2
3
4
5
6
7
8
 {!! Form::model($user, [‘route’ => [‘edit’, $user->id], ‘method’ => ‘PUT’]) !!}

 {!! Form::label(‘titel’, ‘First Name:’!!}
 {!! Form::text(‘titel’, null,) !!}

<button type=”submit”>Update</button>
{!! Form::close() !!}

我的路线:

1
Route::post(‘edit/{id}’, [‘as’ => ‘edit’, ‘uses’ => ‘UserController@editUser’]);

我的控制器:

1
public function editUser($id){};

当点击更新按钮时,我在 RouteCollection.php

中得到 MethodNotAllowedHttpException

我检查了浏览器源代码并看到我使用的 Form::model(..) 生成以下输出:

1
<form method=”POST” action=”http://localhost/myProject/public/edit/1″ accept-charset=”UTF-8″><input name=”_method” type=”hidden” value=”PUT”><input name=”_token” type=”hidden” value=”4nZlyfzzAZmTcZfThQ8gcR6cgEgYgR0ip0JZTKck”>

在表单中有属性method=”POST”,隐藏输入有属性value=”PUT”。这对我来说似乎不正确。有任何想法吗?谢谢


您应该使用”更新”路线来实际保存数据(验证并将其保存到数据库中)。 ‘edit’ 路由是您用来生成编辑表单的路径。

你应该使用PUT方法来运行保存数据的方法。

另外,这里给你一个小提示。了解 RESTful 控制器的工作原理。它们是您在这里做的事情的真正简单方法(绝对值得学习它们):
https://laravel.com/docs/5.1/controllers

  • 我很高兴能帮上忙。 )


你的路线和你的表格不一样。

Laravel 使用隐藏的输入来指定不同的 http 方法,如下所示。

所以在你的路由中你应该使用 put 方法而不是 post。

1
Route::put();

来源:https://www.codenong.com/35535027/

微信公众号
手机浏览(小程序)

Warning: get_headers(): SSL operation failed with code 1. OpenSSL Error messages: error:14090086:SSL routines:ssl3_get_server_certificate:certificate verify failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(): Failed to enable crypto in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(https://static.shanhubei.com/qrcode/qrcode_viewid_9282.jpg): failed to open stream: operation failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57
0
分享到:
没有账号? 忘记密码?