美文网首页
[PHP高可用后端]⑤--新增用户功能

[PHP高可用后端]⑤--新增用户功能

作者: 子木同 | 来源:发表于2017-10-31 15:21 被阅读46次
Paste_Image.png

新建数据库 imooc_app_singwa

Paste_Image.png

创建表 ent_admin_user

Paste_Image.png

database.php

'database'        => 'imooc_app_singwa',
  'prefix'          => 'ent_',
Paste_Image.png

添加后台模板文件add.html

Paste_Image.png

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();
    }
}
Paste_Image.png Paste_Image.png

判断是否是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();
        }
    }
}
Paste_Image.png

validate

Paste_Image.png

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'
    ];
}
Paste_Image.png

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();
        }
    }
}
Paste_Image.png Paste_Image.png

PS:将username字段改为唯一

相关文章

网友评论

      本文标题:[PHP高可用后端]⑤--新增用户功能

      本文链接:https://www.haomeiwen.com/subject/dackpxtx.html