【问题标题】:Laravel 5.4 change passwordLaravel 5.4 更改密码
【发布时间】:2017-12-07 07:48:21
【问题描述】:

我目前正在处理用户可以更改其密码的个人资料页面。我在 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 User 模型的资源路由上面创建了这个新路由

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

每次单击提交按钮时,我都会收到MethodNotAllowedHttpException。我认为问题在于路线,但我不确定。除了发送电子邮件的功能之外,是否还有 Laravel 功能,因为我希望用户在不使用电子邮件的情况下更改其密码。谢谢!

这是我的表格

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

【问题讨论】:

  • 你需要先找到你要更新密码的用户!我的意思是哪个用户正在更新他/她的密码!
  • 您可以使用:$id = \Auth::user()->id; $employee = User::find($id); 这样您就可以获得想要更新密码的用户,这就是您想要的!将此代码放在$employee->password = bcrypt($request->password); 行上方,让我知道这是否有效??!
  • 我认为这是多余的,因为我正在传递我正在更新的对象,我不需要找到它。无论如何,谢谢,我已经通过为更新控制器创建一个新控制器解决了这个问题(这可行,但可能是非常规的)
  • 好的,谢谢您的回复! :)

标签: php laravel laravel-5.4


【解决方案1】:

在您的路线中使用 post 代替 put,如下所示。

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

在您的表单中,您是否传递了“_method”和 CSRF 令牌(CSRF 不是必需的,如果您已禁用它,但我建议始终使用它)?

如果您使用表单和 HTML 类 (ServiceProviders) 在刀片中创建 HTML 和表单,请按以下方式使用。

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

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

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

希望对你有帮助!!

【讨论】:

  • 它不起作用,我认为因为它是一个 POST 方法,它创建一个新模型而不是更新现有模型
【解决方案2】:

您需要在表单字段中添加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>

【讨论】:

  • 我试过了,还是不行,我做的是创建一个UpdatePasswordController,它使用Hash更新当前认证用户的密码
  • 只需在表单中的csrf_field() 之后添加method_field('put')
【解决方案3】:

你给路由设置参数了吗?

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

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

【讨论】:

    【解决方案4】:

    试试这个....

    此代码在我使用 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>
    

    希望我的回答对你有所帮助....

    【讨论】:

      猜你喜欢
      • 2017-07-30
      • 2018-02-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-04
      • 2018-03-27
      • 2017-03-26
      • 2019-07-21
      相关资源
      最近更新 更多