laravel 用户认证简单示例

一、模型代码:

实现接口:\Illuminate\Contracts\Auth\Authenticatable

并引入trait:\Illuminate\Auth\Authenticatable

<?php

namespace App\Model;

use Illuminate\Database\Eloquent\Model;

class Manager extends Model implements \Illuminate\Contracts\Auth\Authenticatable
{
    use \Illuminate\Auth\Authenticatable;
    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()

 

猜你喜欢

转载自www.cnblogs.com/jxl1996/p/10269359.html
0条评论
添加一条新回复
  
今日推荐