thinkphp5.1写个知乎

  一、观察知乎

二、搭建thinkphp5.1,再用bootstrap的组件来修改修改

三、写登陆

1.创建validate

<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/7/23 0023
 * Time: 14:44
 */

namespace app\common\validate;


use think\Validate;

class User extends Validate
{
   protected $rule=[
       "name|姓名"=>['require','length'=>'5,20','chsAlphaNum'],
       "email|邮箱"=>['require','email','unique'=>'zh_user'],
       "mobile|手机号"=>['require','mobile','unique'=>'zh_user'],
       'password|密码'=>['require','length:6,20','alphaNum','confirm']
   ];
}

2.创建表格,不细说了姓名,邮箱,密码,手机号

3.不用全局config,database,玩个新鲜的

<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/7/23 0023
 * Time: 14:41
 */

namespace app\common\model;
use think\Model;

class User extends Model
{
    protected $pk='id';
    protected $table='zh_user';
}

4.写个ajax和insert来引入数据

<script type="text/javascript">
            $(function(){
                $('#register').on('click',function(){
                    //用ajax提交用户信息
                    $.ajax({
                        type: 'post',
                        url: "{:url('insert')}",
                        data: $('#login').serialize(),
                        dataType: 'json',
                        success: function(data){
                            switch (data.status)
                            {
                                case 1:
                                    alert(data.message);
                                    window.location.href = "{:url('index/index')}";
                                    break;
                                case 0:
                                case -1:
                                    alert(data.message);
                                    window.location.back();
                                    break;
                            }

                        }
                    })
                })
            })
        </script>
<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/7/24 0024
 * Time: 9:30
 */

namespace app\index\controller;

use app\common\controller\Base;
use app\common\model\User as UserModel;
use think\Facade\Request;

class User extends Base
{
    public function register()
    {
        $this->assign('title', '用户注册');
        return $this->fetch();
    }

    public function insert()
    {
            $data = Request::post();
            $rule = '\app\common\validate\User';
            $res = $this->validate($data, $rule);
            if (true !== $res) {
                return ['message' => $res, 'status' => -1];
            } else {
                $data = Request::except('password_confirm', 'post');
                if (UserModel::create($data)) {
                    return ['message' => '注册成功', 'status' => 1];
                } else {
                    return ['message' => '注册失败', 'status' => 0];
                }
            }
    }
}

这里遇到一个问题,就是规则验证,要和数据库名称一一对应,$rule里,原先写了username,结果传不进去数据库值,确认密码想用他的验证confirm,必须写name="password_confirm",name另外引入模板use app\common\model\User as UserModel;要看好啊!!!

扫描二维码关注公众号,回复: 2355114 查看本文章

猜你喜欢

转载自blog.csdn.net/weixin_38249353/article/details/81187735