一、创建验证器
创建修改密码验证器
php artisan make:request ChangePasswordRequest
修改生成的验证器:app/Http/Requests/ChangePasswordRequest.php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class ChangePasswordRequest extends FormRequest
{
public function authorize()
{
return true;
}
public function rules()
{
return [
'old_password' => 'required|min:6',
'password' => 'required|min:6|confirmed',
];
}
public function messages()
{
return [
'old_password.required' => '原始密码不能为空',
'old_password.min' => '原始密码不能少于6个字符',
'password.required' => '新密码不能为空',
'password.min' => '新密码不能少于6个字符',
'password.confirmed' => '两次输入的密码不一致',
];
}
}
二、创建控制器
创建密码控制器
php artisan make:controller PasswordController
修改生成的控制器:app/Http/Controllers/PasswordController.php
namespace App\Http\Controllers;
use App\Http\Requests\ChangePasswordRequest;
use Hash;
class PasswordController extends Controller
{
public function password()
{
return view('users.password');
}
public function update(ChangePasswordRequest $request)
{
if(Hash::check($request->get('old_password'), user()->password)){
user()->password = bcrypt($request->get('password'));
user()->save();
flash('密码修改成功', 'success');
return bacK();
}
flash('密码修改失败', 'danger');
return bacK();
}
}
注册路由:修改 routes/web.php
Route::get('password', 'PasswordController@password');
Route::post('password', 'PasswordController@update');
三、创建视图
新建文件:resources/views/users/password.blade.php
访问:http://127.0.0.1:8000/password 进行测试