控制器
<?php
namespace app\admin\controller;
use think\Controller;
use think\Db;
use app\admin\model\Admin as AdminModel;
class Admin extends Controller
{
public function lst()
{
$list = AdminModel::paginate(3);
$this->assign('list',$list);
return $this->fetch();
}
public function add()
{
if(request()->isPost()){
$data=[
'username'=>input('username'),
'password'=>md5(input('password')),
];
$validate = \think\Loader::validate('Admin');
if(!$validate->scene('add')->check($data)){
$this->error($validate->getError());
die;
}
if(db('admin')->insert($data)){
return $this->success('添加管理员成功!','lst');
}else{
return $this->error('添加管理员失败');
}
return;
}
return $this->fetch();
}
public function edit()
{
//获取修改的信息
$id=input('id');
$data = db('admin')->find($id);
$this->assign('data',$data);//模板赋值
//修改后提交
if(request()->isPost()){
$info=[
'id'=>input('id'),
'username'=>input('username'),
'password'=>input('password'),
];
$validate = \think\Loader::validate('Admin');
if($validate->scene('edit')->check($info)){
$info['password'] = md5($info['password']);
$res = \think\Db::name('admin')->update($info);
if($res){
return $this->success('修改管理员成功','lst');
}else{
return $this->error('修改管理员失败');
}
}else{
return $this->error($validate->getError());
}
}
return $this->fetch();
}
public function del()
{
$id = input('id');
if($id == 14){
$this->error('这个是超级管理员,不允许删除');
}else{
$result = db('Admin')->where('id',$id)->delete();
if ($result) {
return $this->success('删除管理员成功');
}else{
return $this->error('删除管理员失败',url('lst'));
}
}
}
}
模型
<?php
namespace app\admin\model;
use think\Model;
class Admin extends Model
{
}
验证
<?php
namespace app\admin\validate;
use think\Validate;
class Admin extends Validate
{
protected $rule = [
'username' => 'require|max:25',
'password' => 'require',
];
protected $message = [
'username.require' => '管理员名称必须填写',
'username.max' => '管理员名称长度不能大于25个字符',
'password.require' => '管理人员密码必须填写',
];
protected $scene = [
'add' => ['username'=>'require','password'],
'edit' => ['username'=>'max:25'],
];
}
视图
lst
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>skalpat</title>
<meta name="description" content="Dashboard">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<!--Basic Styles-->
<link href="__PUBLIC__/style/bootstrap.css" rel="stylesheet">
<link href="__PUBLIC__/style/font-awesome.css" rel="stylesheet">
<link href="__PUBLIC__/style/weather-icons.css" rel="stylesheet">
<!--Beyond styles-->
<link id="beyond-link" href="__PUBLIC__/style/beyond.css" rel="stylesheet" type="text/css">
<link href="__PUBLIC__/style/demo.css" rel="stylesheet">
<link href="__PUBLIC__/style/typicons.css" rel="stylesheet">
<link href="__PUBLIC__/style/animate.css" rel="stylesheet">
</head>
<body>
{include file="common/top"}
<div class="main-container container-fluid">
<div class="page-container">
{include file="common/left"}
<!-- Page Content -->
<div class="page-content">
<!-- Page Breadcrumb -->
<div class="page-breadcrumbs">
<ul class="breadcrumb">
<li>
<a href="#">系统</a>
</li>
<li class="active">管理员管理</li>
</ul>
</div>
<!-- /Page Breadcrumb -->
<!-- Page Body -->
<div class="page-body">
<button type="button" tooltip="添加管理员" class="btn btn-sm btn-azure btn-addon"
onClick="javascript:window.location.href = '{:url('admin/add')}'"><i class="fa fa-plus"></i>
Add
</button>
<div class="row">
<div class="col-lg-12 col-sm-12 col-xs-12">
<div class="widget">
<div class="widget-body&