[英]Symfony \ Component \ HttpKernel \ Exception \ MethodNotAllowedHttpException in update
When I click on update button I get next error: 当我单击更新按钮时,出现下一个错误:
Symfony \\ Component \\ HttpKernel \\ Exception \\ MethodNotAllowedHttpException No message Symfony \\组件\\ HttpKernel \\异常\\ 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
In your action attribute you specified a direct call to the controller without going to the route URI. 在action属性中,您指定了对控制器的直接调用,而无需访问路由URI。 Therefore address the action attribute to this
因此,将动作属性处理为此
<form method="post" action="edit/user/{{$id}}" >
It will go through the route and validates the CSRF then proceeds to the request 它将通过路线并验证CSRF,然后继续进行请求
您的路线应为“张贴”而不是“张贴”
Route::put('edit/user/{id}','UsersController@update’)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.