新建数据库 imooc_app_singwa
创建表 ent_admin_user
database.php
'database' => 'imooc_app_singwa',
'prefix' => 'ent_',
添加后台模板文件add.html
add.html
<!--header-->
{include file="public/_meta" /}
<article class="page-container">
<form class="form form-horizontal" id="form-admin-add" method="post" action="{:url('admin/add')}">
<div class="row cl">
<label class="form-label col-xs-4 col-sm-3"><span class="c-red">*</span>管理员名:</label>
<div class="formControls col-xs-8 col-sm-9">
<input type="text" class="input-text" value="" placeholder="" id="username" name="username">
</div>
</div>
<div class="row cl">
<label class="form-label col-xs-4 col-sm-3"><span class="c-red">*</span>密码:</label>
<div class="formControls col-xs-8 col-sm-9">
<input type="password" class="input-text" autocomplete="off" value="" placeholder="密码" id="password" name="password">
</div>
</div>
<div class="row cl">
<div class="col-xs-8 col-sm-9 col-xs-offset-4 col-sm-offset-3">
<input class="btn btn-primary radius" type="submit" value=" 提交 ">
</div>
</div>
</form>
</article>
{include file="public/_footer" /}
</body>
</html>
如果菜单展开失效
_footer.html中添加
<script>
$(function () {
$(".Hui-aside").Huifold({
titCell: '.menu_dropdown dl dt',
mainCell: '.menu_dropdown dl dd',
});
});
</script>
Admin.php
<?php
/**
* Created by PhpStorm.
* User: tong
* Date: 2017/10/26
* Time: 14:58
*/
namespace app\admin\controller;
use \think\Controller;
class Admin extends Controller
{
public function add()
{
return $this->fetch();
}
}
判断是否是post提交
request()->isPost()
获取post数据
input('post.')
<?php
namespace app\admin\controller;
use think\Controller;
class Admin extends Controller
{
public function add()
{
//判断是否是post提交
if (request()->isPost()) {
//打印提交的数据
dump(input('post.'));
} else {
return $this->fetch();
}
}
}
validate
AdminUser.php(validate)
<?php
namespace app\common\validate;
use think\Validate;
class AdminUser extends Validate
{
protected $rule = [
'username' => 'require|max:20',
'password' => 'require|max:20'
];
}
AdminUser.php(model)
<?php
/**
* Created by PhpStorm.
* User: tong
* Date: 2017/10/31
* Time: 14:45
*/
namespace app\common\model;
use think\Model;
class AdminUser extends Model
{
//自动插入时间
protected $autoWriteTimestamp = true;
/**
* 新增
* @param $data
* @return mixed
*/
public function add($data)
{
if (!is_array($data)) {
exception("传递数据不合法");
}
//allowField 如果某个字段在表没有可过滤
$this->allowField(true)->save($data);
return $this->id;
}
}
Admin.php(controller)
<?php
namespace app\admin\controller;
use think\Controller;
class Admin extends Controller
{
public function add()
{
//判断是否是post提交
if (request()->isPost()) {
$data = input('post.');
//validate
$validate = validate('AdminUser');
//halt($validate->check($data));//boolean false
if (!$validate->check($data)) {
$this->error($validate->getError());
}
$data['password'] = md5($data['password'] . '_#sing_ty');
$data['status'] = 1;
//1 exception
try {
$id = model('AdminUser')->add($data);
} catch (\Exception $e) {
echo $e->getMessage();
}
if ($id) {
$this->success('id=' . $id . "的用户创建成功");
} else {
$this->error('error');
}
} else {
return $this->fetch();
}
}
}
PS:将username字段改为唯一