摘要:Login.php<?php namespace app\admin\controller; use think\Controller; use app\admin\model\UserModel; use think\facade\View; use think\facade\Request; use think\facade\S
Login.php
<?php
namespace app\admin\controller;
use think\Controller;
use app\admin\model\UserModel;
use think\facade\View;
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();
$username=$data['username'];
//从数据库取出数据
$user=UserModel::where('username',$username)->find();
if($user!=true){
$info=['res'=>0,'msg'=>'用户名不存在'];
}elseif($data['password']!=$user['password']){
$info=['res'=>0,'msg'=>'密码输入不正确'];
}else{
$info=['res'=>1,'msg'=>'登陆成功'];
Session::set('username',$user['username']);
}
return $info;
}
//退出操作
public function loginOut(){
Session::delete('username');
$this->redirect('login');
}
}
?>login.html
{include file="/public/head"}
<body class="login-bg">
<div class="login layui-anim layui-anim-up">
<div class="message">PHP中文网后台管理系统登录</div>
<div id="darkbannerwrap"></div>
<form method="post" class="layui-form" >
<input name="username" placeholder="用户名" type="text" id="username" lay-verify="required" class="layui-input" >
<hr class="hr15">
<input name="password" lay-verify="required" placeholder="密码" id="password" type="password" class="layui-input">
<hr class="hr15">
<input value="登录" lay-submit lay-filter="login" style="width:100%;" type="submit">
<hr class="hr20" >
</form>
</div>
<script>
$(function () {
layui.use('form', function(){
var form = layui.form;
// layer.msg('玩命卖萌中', function(){
// //关闭后的操作
// });
//监听提交
form.on('submit(login)', function(data){
// alert(888)
$.post('{:url(\'doLogin\')}',{
'username':$('#username').val(),
'password':$('#password').val()
},function(data){
if(data.res==1){
layer.msg(data.msg,function(){
location.href='{:url(\'Index/index\')}'
});
}
layer.msg(data.msg,function(){
});
})
return false;
});
});
})
</script>
</body>
</html>建立common.php 主要目的就是进行对登录进行验证,验证地方有admin.php/User/index , admin.php/Index/index
<?php
namespace app\admin\controller;
use think\Controller;
use think\facade\View;
use think\facade\Session;
class Common extends Controller{
public function __construct(){
parent::__construct();
if(!Session::has('username')){
$this->error('你还没登录,请返回登录','Login/login');
}
}
}
?>Index.php Index 继承common控制器
<?php
namespace app\admin\controller;
use think\Controller;
use app\admin\controller\Common;
use think\facade\View;
class Index extends Common
{
public function index()
{
return $this->view->fetch();
}
public function welcome(){
return $this->view->fetch();
}
}User.php user 同样继承common 类
<?php
namespace app\admin\controller;
use think\Controller;
use think\facade\View;
use app\admin\model\UserModel;
use think\facade\Request;
use app\admin\controller\Common;
class User extends Common
{
public function index()
{
//$user = new UserModel();
//按id进行排序,并且每一页设置八条数据
//$users=$user->order('id','desc')->paginate(8);
//将数据赋值给模板
$users=UserModel::order('id','desc')->paginate(5);
$this->assign('users',$users);
//$this->view->users=$users;
return $this->view->fetch();
}
批改老师:天蓬老师批改时间:2018-11-23 09:03:57
老师总结:通过公共控制器,添加共享成员,这是开发的一个基本技巧,不错,你掌握了