簡體   English   中英

Laravel 5.4更改密碼

[英]Laravel 5.4 change password

我目前正在個人資料頁面上工作,用戶可以在其中更改密碼。 我在UserController中創建了一個新函數changePassword()

public function changePassword(Request $request, User $employee) {
    $validator = Validator::make($data, [
        'password' => 'required|string|min:6|confirmed',
        'password_confirmation' => 'required|string|min:6|same:password'
    ]);

    if ($validator->fails()) {
        Session::flash('error', "Fill-out the form correctly. Try again!");
        return redirect()->back()->withErrors($validator);
    }

    $employee->password = bcrypt($request->password);
    $employee->save();
    return view('users.show', ['employee'=>$employee]);
}

我在web.php中在用戶模型的資源路由上方創建了此新路由

Route::put('users/{user}', 'UserController@changePassword')->name('users.changePassword');

每次單擊提交按鈕時,都會收到MethodNotAllowedHttpException 我認為問題在於路線,但我不確定。 除了發送電子郵件的功能之外,還有Laravel功能嗎,因為我希望用戶更改密碼而不使用電子郵件。 謝謝!

這是我的表格

{!! Form::model($employee, ['method'=>'PUT', 'route'=>['users.changePassword', $employee]]) !!}

代替放置,請在下面的路線中使用post。

Route::post('users/{user}', 'UserController@changePassword')->name('users.changePassword');

在您的表單中,您是否傳遞了“ _method”和CSRF令牌(如果您已禁用它,則不需要CSRF,但我建議始終使用它)?

如果您使用Form和HTML類(ServiceProviders)在刀片中創建HTML和Forms,請使用以下內容。

{!! Form::model($employee, ['method' => 'POST','route' => ['users.changePassword']]) !!}

--- Your Fields Code will go here ----

{!! Form::close() !!}

希望能幫助到你!!

您需要在表單字段中添加PUT http動詞。 如果您使用的是laravelcollective:

{!! Form::model($employee, ['method' => 'put','route' => 
['users.changePassword']]) !!}

--- Your Fields Code will go here ----

{!! Form::close() !!}

要么

<form action="your-url" method="POST">

 {{ method_field('PUT') }}

</form>

是否設置路徑參數?

{!! Form::Model($user, ['action' => ['UserController@changePassword', $user->id],'method' => 'PUT']) !!}

{!! Form::close() !!}

嘗試這個....

這段代碼在我的項目中使用laravel 5.3

ubahprofilecontroller.php

public function updatePwd($id, Request $request)
       {
         //cek password lama
           $messages = array(
               'password_lama.required'=>'Harap masukkan password',
               'password_baru.required' => 'Password baru tidak boleh kosong',
               'ulangi_password.required' => 'Harap ketikkan ulang password baru',
               'ulangi_password.same' => 'Password baru dan konfirmasi password tidak cocok'
           );

           $rules = array (
             'password_lama'=> 'required',
             'password_baru'=> 'required',
             'ulangi_password'=> 'required|same:password_baru'
           );
           $validator = Validator::make ( Input::all (), $rules, $messages );

           if($validator->fails())
             {
                 /*return Redirect('edit_password')
                     ->withErrors($validator);*/
                     $password ='password';
                     return redirect('ubahPwd')->withErrors($validator)->withInput();
             }
           else
             {
               $check = User::where('id',$id)->first();
               if (Input::get('password_lama') == $check->value)
               {
                       if(Input::get('password_baru') == Input::get('ulangi_password'))
                         {
                           $check -> password = bcrypt(Input::get('password_baru'));
                           $check -> salt_password = Input::get('password_baru');
                           // save our duck
                           $check->save();

                             /*$msg = array('msg' => 'Password changed Successfully');*/
                             return redirect('ubahPwd')->with('success','Password berhasil diubah');
                         }
                         else
                         {
                             /*$msg = array('msg' => 'New password and Confirm password did not match');*/
                             return redirect('ubahPwd')->with('salah','Password baru dan konfirmasi password tidak sama');
                         }
               }
               else
               {
                 /*$msg = array('msg' => 'Current password is incorrect');*/
                 /*return Redirect('edit_password')
                               ->with('status-failed', 'Current password is incorrect');*/
                    return redirect('ubahPwd')->with('salah','Password lama salah');
               }
             }
      }

profile.blade.php

 <form class="form-horizontal" action="{{ url('/ubahPassword/update',Auth::user()->id )}}" method="POST">
              <<div class="content" style="padding-left:50px;padding-right:50px">

                <div class="form-group">
                    {{ csrf_field() }}
                  <label for="id" class="col-sm-3 control-label">ID</label>
                  <div class="col-sm-2">
                    <input type="text" class="form-control" id="id"  name="id" value="{{ Auth::user()->id }}" readonly>
                  </div>
                </div>

                <div class="form-group">
                  <label for="username" class="col-sm-3 control-label">Password Lama</label>
                  <div class="col-sm-6">
                    <input type="password" class="form-control" id="username" name="password_lama">
                    @if ($errors->has('password_lama')) <p class="help-block" style="color:red;">{{ $errors->first('password_lama') }}</p> @endif
                  </div>
                </div>

                <div class="form-group">
                  <label for="inputEmail3" class="col-sm-3 control-label">Password baru</label>
                  <div class="col-sm-5">
                    <input type="password" class="form-control" id="email" name="password_baru">
                    @if ($errors->has('password_baru')) <p class="help-block" style="color:red;">{{ $errors->first('password_baru') }}</p> @endif
                  </div>
                </div>

                <div class="form-group">
                  <label for="inputEmail3" class="col-sm-3 control-label">Ulangi Password</label>
                  <div class="col-sm-5">
                    <input type="password" class="form-control" id="email" name="ulangi_password">
                    @if ($errors->has('ulangi_password')) <p class="help-block" style="color:red;">{{ $errors->first('ulangi_password') }}</p> @endif
                  </div>
                </div>

              <div class="form-group">
                <button type="submit" class="btn btn-info pull-right col-sm-3">Simpan</button>
              </div>
            </div>
          </form>

希望我的回答對您有所幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM