laravel Symfony \ Component \ HttpKernel \ Exception \ MethodNotAllowedHttpException 更新中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/50454466/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Symfony \ Component \ HttpKernel \ Exception \ MethodNotAllowedHttpException in update
提问by befreeforlife
When I click on update button I get next error:
当我单击更新按钮时,出现下一个错误:
Symfony \ Component \ HttpKernel \ Exception \ MethodNotAllowedHttpException No message
Symfony \ Component \ HttpKernel \ Exception \ MethodNotAllowedHttpException 无消息
For update I have next routes:
对于更新,我有下一条路线:
Route::post('edit/user/{id}','UsersController@update');
Route::get('edit/user/{id}','UsersController@edit');
As you see, I use post method for update.
如您所见,我使用 post 方法进行更新。
My controller code:
我的控制器代码:
public function edit($id){
$user = User::where('id',$id)
->first();
return view('user.edit', compact('user','id'));
}
public function update(Request $request, $id){
$user = new User();
$data_user = $this->validate($request,$rules_user);
$data_user['id'] = $id;
$user->updateUser($data_user);
return redirect('/users');
}
My update.blade.php code:
我的 update.blade.php 代码:
@extends('layouts.app')
@section('content')
<div class="container">
@if ($errors->any())
<div class="alert alert-danger">
<ul>
@foreach ($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
</div><br />
@endif
<div class="row">
<form method="post" action="{{action('UsersController@update', $id)}}" >
{{csrf_field()}}
<input name="_method" type="hidden" value="PATCH">
<div class="form-group">
<input type="hidden" value="{{csrf_token()}}" name="_token" />
<label for="name">User name:</label>
<input type="text" class="form-control" name="name" value={{$user->name}} />
</div>
<button type="submit" class="btn btn-primary">Update</button>
</form>
</div>
</div>
@endsection
回答by Grayback
In your action attribute you specified a direct call to the controller without going to the route URI. Therefore address the action attribute to this
在您的 action 属性中,您指定了对控制器的直接调用,而无需转到路由 URI。因此,将 action 属性寻址到此
<form method="post" action="edit/user/{{$id}}" >
It will go through the route and validates the CSRF then proceeds to the request
它将通过路由并验证 CSRF 然后继续请求
回答by Mohamed Allam
Your route should be Put instead of post
你的路线应该是 Put 而不是 post
Route::put('edit/user/{id}','UsersController@update')