zoukankan      html  css  js  c++  java
  • 【项目练习】thinkphp用户注册

    使用mvc,ajax

    路由

    //后台登陆
    Route::group('admin', function () {
        Route::rule('login', 'admin/Index/login');
        //注册
        Route::rule('reg', 'admin/Index/reg');
    });

    ajax

                    $.ajax({
                        url: '{:url("admin/index/reg")}',
                        type: 'post',
                        data: $('form').serialize(),
                        dataType: 'json',
                        success: function (res) {
                            if (res.code == 1) {
                                // console.log(11);
                                layer.msg(res.msg, {
                                    iocn: 6,
                                    time: 2000
                                }, function () {
                                    location.href = res.url;
                                })
                            } else {
                                layer.msg(res.msg, {
                                    iocn: 6,
                                    time: 2000
                                })
                            }
                        }
                    })

    控制器

     public function reg()
      {
        if (request()->isAjax()) {
          $data = [
            'username' => input('post.username'),
            'pwd' => input('post.pwd'),
            'nickname' => input('post.nickname'),
            'email' => input('post.email')
          ];
          $res = model('Users')->reg($data);
          if ($res == 1) {
            $this->success('注册成功', 'admin/index/login');
          } else {
            $this->error('失败!');
          }
        };
        return view('reg');
      }

    模型

    <?php
    
    namespace appcommonmodel;
    
    use thinkModel;
    use traitsmodelSoftDelete;
    
    class Users extends Model
    {
      //软删除
      use SoftDelete;
    
      //注册测试
      public function reg($data)
      {
        //创建验证器
        $val = new appcommonvalidAdmin();
        if (!$val->scene('reg')->check($data)) {
          return $val->getError();
        }
        $res = $this->save($data);
        if ($res) {
          return 1;
        } else {
          return '注册失败';
        }
      }
    }

    验证器

    <?php
    
    namespace appcommonvalid;
    
    use thinkValidate;
    
    class Admin extends Validate
    {
      protected $rule = [
        'username'  => 'require',
        'pwd' => 'require',
        'nickname' => 'require',
        'email' => 'require|email',
      ];
    
      //登陆验证场景
      public function login()
      {
        return $this->only(['username', 'pwd']);
      }
      //注册场景
      public function sceneReg()
      {
        return $this->only(['username', 'pwd', 'nickname', 'email']);
      }
      //验证提示
      protected $message  =   [
        'username.unique' => '栏目名称不能重复',
        'username.require' => '用户名必须',
        'pwd.require' => '密码必须',
      ];
    }
  • 相关阅读:
    Python-快速入门
    Python-面向对象编程
    python-模块
    .net mvc onexception capture; redirectresult;
    a c lang in linux
    上海哪里有学陈氏太极拳?
    【Origin】 叹文
    【Origin】 碑铭
    【Origin】 偶题 之 抒意
    【Origin】答友朋关切书
  • 原文地址:https://www.cnblogs.com/xiaozhang666/p/11555785.html
Copyright © 2011-2022 走看看