摘要:通过本章的学习,实现了登录模块,控制器代码如下:<?php namespace app\admin\controller; use app\admin\model\user\UserModel; use think\Controller; use think\facade\Request; use think\facade\Sessio
通过本章的学习,实现了登录模块,控制器代码如下:
<?php
namespace app\admin\controller;
use app\admin\model\user\UserModel;
use think\Controller;
use think\facade\Request;
use think\facade\Session;
class Login extends Controller
{
public function Login()
{
return $this->view->fetch();
}
public function doLogin()
{
$data = Request::param();
$res= UserModel::where('username', $data['username'])->find();
if(!$res)
{
return ['res'=>'0','msg'=>'用户名不存在'];
}
if($data['password']==$res->password)
{
Session::set('user',$res);
return ['res'=>'1','msg'=>'登录成功'];
}else
{
return ['res'=>'0','msg'=>'用户名或密码错误,请重新输入'];
}
}
}通过新增公共控制器类检验用户是否登录:
<?php
namespace app\admin\controller;
use think\App;
use think\Controller;
use think\facade\Session;
class Check extends Controller
{
public function __construct(App $app = null)
{
parent::__construct($app);
if(!Session::has('user'))
{
$this->error('您还未登录,请先登录','Login/Login');
}
}
}效果图:


批改老师:天蓬老师批改时间:2019-05-14 17:49:44
老师总结:如果你用了模型, 就尽可能使用模型中的静态方法, 例如get来代替:$res= UserModel::where('username', $data['username'])->find();
