基于 tp5.1 的如何简单优雅的书写代码?

关于新手第一次使用tp,login模块简单写法,请大家指正哇 :joy:

login 模块 三个方法 login loginCheck loginOut

1.login控制器代码


  public function login()
    {
        if(Admin::adminUser()){
            return redirect('/admin');
        }else{
            if(Admin::getRemember()){
                $data = Admin::getRemember();
                return view('login/login',compact('data'));
            }
            return view('login/login');
        }
    }

    public function loginCheck(Request $request, AdminUser $user)
    {
        $data = $request->post();

        $validate = new LoginCheck();

        if (!$validate->check($data)) {
            return msg('error', 500, $validate->getError());
        }
        $result = $user->verify_pwd($data);

        return $result;
    }

    public function loginOut()
    {

        Admin::loginOut();
        return redirect('/')->with('success','退出登录');
    }

2.Admin.php类


<?php

namespace app\common;
use think\facade\Cookie;
use think\facade\Session;

class Admin
{
    //操作验证session、cookie 信息  语义化代码
    public static function adminUser()
    {
        $user = Session::get(config('app.user_info_session'));
        return $user;
    }

    public static function loginOut()
    {
        Session::delete(config('app.user_info_session'));
        return true;
    }
    public static function remember($data)
    {
        Cookie::set('admin_user',$data);
    }
    //记住我
    public static function getRemember()
    {
        $data = Cookie::get('admin_user');
        return $data;
    }
    public static function delRemember()
    {
        $data = Cookie::delete('admin_user');
        return $data;
    }
}

3.瘦控制器 胖模型的思想 模型封装验证方法

<?php

namespace app\admin\model;


use app\common\Admin;
use think\facade\Session;
use think\Model;


class AdminUser extends Model
{
    protected $table = 'admin_users';

    public static $password_salt = '';


    protected static function generateSalt()
    {
        $str = 'qwertyuiopasdfghjklzxvcbnm1234567890QWAESRDTFYGUHIJOKPLMNBVCXZ';
        $salt = substr(str_shuffle($str), 10, 6);
        return $salt;
    }

    protected static function init()
    {
        parent::init();
        self::$password_salt = self::generateSalt();
    }

    public  function verify_pwd($data)
    {
       $user = $this->where('username',$data['username'])->where('password',md5($data['password']))->find();
       if(!empty($user)){
           self::Admin($user);
          if($data['remember']=='on'){
              Admin::remember($data);
          }
           return msg('success',200,'登录成功');
       }else{
           return msg('error',500,'账号或密码不存在');
       }
    }

    public static function Admin($user=null)
    {
         Session::set(config('app.user_info_session'),$user);
    }
}

4.在common.php 公共文件下 定义公共函数 如:

function msg($title,$code=500,$message=''){
    $msg = [
        'title' => $title,
        'code' => $code,
        'message' => $message
    ];
    return json_encode($msg);
}

(ps):谢谢大家 :joy: 观摩咯

不成大牛,不改個簽
讨论数量: 8

写得不错。有一点是方法名是否统一一下,使用小驼峰呢?

4年前 评论
Latent (楼主) 4年前

php 验证没那么麻烦, password 加密 。你可以用PHP内置函数 password_hash()

4年前 评论
Latent (楼主) 4年前
qinplain (作者) 4年前
Latent (楼主) 4年前

这部分代码可以这么优化一下

  public function login()
    {
        if(Admin::adminUser()){
            return redirect('/admin');
        }
            if(Admin::getRemember()){
                $admin = Admin::getRemember();  //变量名不规范,data不能表明取出数据的意义
                return view('login/login',compact('admin'));
            }
            return view('login/login');
    }
4年前 评论
Latent (楼主) 4年前
hooklife (作者) 4年前

verify_pwd
Admin

这种命名要避免,使用统一的命名

4年前 评论

json_encode()第二个参数啥意思
我是小白。 :grin:
我百度不出来true的说法。。

4年前 评论
Latent (楼主) 4年前

《PHP PSR 标准规范》
https://www.kancloud.cn/manual/thinkphp5_1...
建议先看php的标准规范, 在结合框架要求的开发规范来书写.

4年前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!