我想为登录的用户添加密码更新选项,因此我使用了以下代码
控制器auth\authController.php
public function updatePassword()
{
$user = Auth::user();
$rules = array(
'old_password' => 'required',
'password' => 'required|alphaNum|between:6,16|confirmed'
);
$validator = Validator::make(Input::all(), $rules);
if ($validator->fails()) {
return Redirect::route('change-password', $user->id)->withErrors($validator);
} else {
if (!Hash::check(Input::get('old_password'), $user->password)) {
return Redirect::route('change-password', $user->id)->withErrors('Your old password does not match');
} else {
$user->password = Input::get('password');
$user->save();
return Redirect::route('change-password', $user->id)->with("message", "Password have been changed");
}
}
}路线
Route::post('change-password', 'Auth\AuthController@updatePassword');
Route::get('change-password', 'Auth\AuthController@updatePassword');即时消息收到以下错误
FatalErrorException in AuthController.php line 123:
Class 'App\Http\Controllers\Auth\Auth' not found对于行"$user = Auth::user();“
发布于 2016-08-27 05:31:54
你的问题有隐藏的答案..我有类似的问题@faz..实际上我已经用他的问题的代码做了这个把戏
实现这一目标的正确方法- -
protected function postChangePassword(ChangePasswordFormRequest $request){
$user = Auth::user();
$current_password = Input::get('current_password');
$password = bcrypt(Input::get('password'));
$user_count = DB::table('users')->where('id','=',$this->user_id)->count();
if (Hash::check($current_password, $user->password) && $user_count == 1) {
$user->password = $password;
try {
$user->save();
$flag = TRUE;
}
catch(\Exception $e){
$flag = FALSE;
}
if($flag){
return redirect('/u/0/change/password')->with('success',"Password changed successfully.");
}
else{
return redirect('/u/0/change/password')->with("danger","Unable to process request this time. Try again later");
}
}
else{
return redirect('/u/0/change/password')->with("warning","Your current password do not match our record");
}
}请注意,对于哈希和身份验证,我们需要包括顶部的类和我通过构造函数$this->user_id = Auth::user()->id;获得的user_id。我想我已经帮助了很多人。
发布于 2015-08-04 16:17:57
其名称空间问题,请尝试:
//if this method is not protected by a middleware for only authenticated users
//verify that user is currently logged in:
if(!$user = \Auth::user()) abort(503); //or return redirect()->route('login')
$rules = array(
'old_password' => 'required',
'password' => 'required|alphaNum|between:6,16|confirmed'
);或者在AuthController的顶部添加命名空间
use Auth;
class AuthController{
....
}发布于 2015-08-04 16:19:09
您没有导入Auth类。
将此代码添加到文件的顶部。在命名空间之后。
使用Illuminate\Support\Facades\Auth;
https://stackoverflow.com/questions/31803973
复制相似问题