一、模型代码:
实现接口:IlluminateContractsAuthAuthenticatable
并引入trait:IlluminateAuthAuthenticatable
<?php
namespace AppModel;
use IlluminateDatabaseEloquentModel;
class Manager extends Model implements IlluminateContractsAuthAuthenticatable
{
use IlluminateAuthAuthenticatable;
protected $table = 'manager';
}
二、控制器代码:
public function check(Request $request)
{
if ($request->method() == 'POST') {
//判断验证码是否正确
$this->validate($request, [
'captcha' => 'required|captcha'
]);
$data = $request->only(['username', 'password']);
$data['status'] = 1;
//用户认证
$result = Auth::guard('admin')->attempt($data, $request->get('online'));
if ($result) {
//成功 跳转到后台首页
return redirect('/admin/index/index');
} else {
//失败 跳转回登录页
return redirect('/admin/public/login')->withErrors([
'loginError' => '用户名或密码错误。'
]);
}
}
}
public function logout()
{
Auth::guard('admin')->logout();
return redirect('/admin/public/login');
}
三、认证成功后获取用户信息
Auth::guard('admin')->user()