我正在尝试更改管理员密码,但它说:未定义路由[/admin/update-pwd]。如何更改它?
控制器:
public function changepassword()
{
return view('admin.changepassword');
}
public function chkPassword(Request $request){
$data = $request->all();
$adminCount = Admin::where(['username' => Session::get('adminSession'),'password'=>md5($data['current_pwd'])])->count();
if ($adminCount == 1) {
echo "true"; die;
} else {
echo "false"; die;
}
}
public function updatePassword(Request $request){
if($request->isMethod('post')){
$data = $request->all();
$adminCount = Admin::where(['username' => Session::get('adminSession'),'password'=>md5($data['current_pwd'])])->count();
if ($adminCount == 1) {
$password = md5($data['new_pwd']);
Admin::where('username',Session::get('adminSession'))->update(['password'=>$password]);
return redirect('/admin/settings')->with('flash_message_success', 'Password updated successfully.');
}else{
return redirect('/admin/settings')->with('flash_message_error', 'Current Password entered is incorrect.');
}
}
}
刀片文件:
<form method="POST" action="{{ route('/admin/update-pwd') }}" enctype="multipart/form-data">
@csrf
@method('PATCH')
<strong class="text-muted d-block mb-2">Enter Current Password</strong>
<div class="input-group mb-3">
<div class="input-group input-group-seamless">
<input type="password" name="current_pwd" class="form-control @error('password') is-invalid @enderror" id="form2-password" placeholder="Password">
<span class="input-group-append">
<span class="input-group-text">
<i class="material-icons">lock</i>
</span>
</span>
</div>
</div>
<strong class="text-muted d-block mb-2">Enter New Password</strong>
<div class="input-group mb-3">
<div class="input-group input-group-seamless">
<input type="password" name="new_pwd" class="form-control @error('password') is-invalid @enderror" id="form2-password" placeholder="Password">
<span class="input-group-append">
<span class="input-group-text">
<i class="material-icons">lock</i>
</span>
</span>
</div>
</div>
<strong class="text-muted d-block mb-2">Confirm New Password</strong>
<div class="input-group mb-3">
<div class="input-group input-group-seamless">
<input type="password" name="confirm_pwd" class="form-control @error('password') is-invalid @enderror" id="form2-password" placeholder="Password">
<span class="input-group-append">
<span class="input-group-text">
<i class="material-icons">lock</i>
</span>
</span>
</div>
</div>
@error('password')
<div class="small text-danger">{{ $message }}</div>
@enderror
<button type="submit" class="mb-2 btn btn-primary mr-2">Update Password</button>
</form>
路线:
Route::get('/admin/changepassword', 'ChangepasswordController@changepassword');
Route::get('/admin/check-pwd','AdminController@chkPassword');
Route::match(['get', 'post'],'/admin/update-pwd','AdminController@updatePassword');
当您使用route
帮助程序时,它希望您提供路由名称而不是 URI,因此请添加以下内容:
Route::match(['get', 'post'],'/admin/update-pwd','AdminController@updatePassword')
->name('admin.update_password');
然后,在您看来,您可以使用:
route('admin.update_password');
或者改用url
帮助程序方法:
// instead of this
{{ route('/admin/update-pwd') }}
//use
{{ url('/admin/update-pwd') }}
您正在尝试在表单方法中使用 Laravel命名路由。使用"命名路由的 Laravel",并在窗体中定义它。
网站内部.php
Route::post('/admin/update-pwd','AdminController@updatePassword')->name('admin/update-pwd');
然后,您将能够使用内部表单
<form method="POST" action="{{ route('/admin/update-pwd') }}" enctype="multipart/form-data">