NIUCLOUD是一款SaaS管理后台框架多应用插件+云编译。上千名开发者、服务商正在积极拥抱开发者生态。欢迎开发者们免费入驻。一起助力发展! 广告
# :-: 代码开发规范 ***** ## 命名规范 `ThinkPHP6.0`遵循`PSR-2`命名规范和`PSR-4`自动加载规范,并且注意如下规范: ### 目录和文件 * 目录使用小写+下划线; * 类库、函数文件统一以`.php`为后缀; * 类的文件名均以命名空间定义,并且命名空间的路径和类库文件所在路径一致; * 类(包含接口和Trait)文件采用驼峰法命名(首字母大写),其它文件采用小写+下划线命名; * 类名(包括接口和Trait)和文件名保持一致,统一采用驼峰法命名(首字母大写); ### 函数和类、属性命名 * 类的命名采用驼峰法(首字母大写),例如 `User`、`UserType`; * 函数的命名使用小写字母和下划线(小写字母开头)的方式,例如 `get_client_ip`; * 方法的命名使用驼峰法(首字母小写),例如 `getUserName`; * 属性的命名使用驼峰法(首字母小写),例如 `tableName`、`instance`; * 特例:以双下划线`__`打头的函数或方法作为魔术方法,例如 `__call` 和 `__autoload`; ### 常量和配置 * 常量以大写字母和下划线命名,例如 `APP_PATH`; * 配置参数以小写字母和下划线命名,例如 `url_route_on` 和`url_convert`; * 环境变量定义使用大写字母和下划线命名,例如`APP_DEBUG`; ### 数据表和字段 * 数据表和字段采用小写加下划线方式命名,并注意字段名不要以下划线开头,例如 `think_user` 表和 `user_name`字段,不建议使用驼峰和中文作为数据表及字段命名。 **请理解并尽量遵循以上命名规范,可以减少在开发过程中出现不必要的错误。** ### 路由写法规范 ~~~ Route::group('v1',function () { //小程序用户登录 Route::post('login', 'v1.MiniProgram/login'); // 获取小程序的配置信息 Route::get('config', 'v1.MiniProgram/config'); // 获取用户登录状态 Route::get('checkLogin', 'v1.MiniProgram/checkLogin'); //公众号 h5 APP通用的api登录接口 Route::post('commonLogin', 'v1.App/commonLogin'); //公众号 h5 APP通用的api注册接口 Route::post('commonReg', 'v1.App/commonReg'); //小程序绑定公众号 Route::get('bindOfficial', 'v1.MiniProgram/bindOfficial'); //API接口微信公众号授权登录 Route::get('officialLogin','v1.App/officialLogin'); })->middleware(buwang\middleware\Login::class, false)->append(['bwsaas'=>1]); Route::group(function () { // 判断是不是应用的管理员 Route::get('v1/isManage', 'v1.MiniProgram/isManage'); // 获取ucode邀请码对应的用户信息 Route::get('v1/getUcodeUser', 'v1.MiniProgram/getUcodeUser'); // 绑定小程序端获取到的手机号 Route::post('v1/bindWechatMobile', 'v1.MiniProgram/bindWechatMobile'); // 获取绑定手机验证码 Route::post('v1/getMobileSms', 'v1.MiniProgram/getMobileSms'); // 获取默认地址 Route::get('v1/user/getDefaultAddress', 'v1.User/getDefaultAddress'); // 获取地址列表 Route::get('v1/user/getAddressList', 'v1.User/getAddressList'); // 地址删除 Route::post('v1/user/delAddress', 'v1.User/delAddress'); // 获取user用戶信息 Route::get('v1/user/getUserInfo', 'v1.User/getUserInfo'); // 保存或修改我的地址 Route::post('v1/user/addAddress', 'v1.User/addAddress'); // 验证是否设置安全密码 Route::get('v1/user/isSafePassword', 'v1.User/isSafePassword'); // 检查旧的安全密码 Route::post('v1/user/checkSafePassword', 'v1.User/checkSafePassword'); // 设置安全密码6位数字 Route::post('v1/user/setSafePassword', 'v1.User/setSafePassword'); // 验证是否绑定手机号 Route::get('v1/user/isBandMobile', 'v1.User/isBandMobile'); })->middleware(buwang\middleware\Login::class, true)->append(['bwsaas'=>1]); ~~~ * 上面就是一些规范的参考;所有的路由第一个参数: 统一用小写和小驼峰加/的书写形式。如v1/user/addAddress,这里面的addAddress就要使用小驼峰命名方式,如果v1/user/edit,这里面的edit是一个独立的单词,就可以直接使用小写书写。第二个参数:严格按照目录小写,类名大驼峰,方法小驼峰;controller如果有多层目录,中间有.号连接, * 注意:不允许用.号写法如: ~~~ Route::get('admin.Plugin/index', 'admin.Plugin/index');//不符合框架统一要求规范 ~~~ 还有一种坚决不允许的错误写法如 ~~~ //反面例子 Route::get('Config/configindex', 'Config/configindex');//应该为Route::get('config/index', 'Config/configIndex'); Route::post('Config/getconfiglist', 'Config/getconfiglist'); Route::rule('Config/editconfig', 'Config/editconfig'); Route::post('Config/setconfigshow', 'Config/setconfigshow'); Route::post('Config/configsoftdleting', 'Config/configsoftdleting'); ~~~ ### API接口返回格式说明 在基类控制器\buwang\base\BaseController中,封装的API接口数据返回格式方法,也有模板渲染SSR返回封装 ~~~ <?php // +---------------------------------------------------------------------- // | Bwsaas // +---------------------------------------------------------------------- // | Copyright (c) 2015~2020 http://www.buwangyun.com All rights reserved. // +---------------------------------------------------------------------- // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 ) // +---------------------------------------------------------------------- // | Gitee ( https://gitee.com/buwangyun/bwsaas ) // +---------------------------------------------------------------------- // | Author: buwangyun <hnlg666@163.com> // +---------------------------------------------------------------------- // | Date: 2020-9-28 10:55:00 // +---------------------------------------------------------------------- declare (strict_types=1); namespace buwang\base; use buwang\traits\JsonTrait; use buwang\traits\JwtTrait; use buwang\traits\JumpTrait; use think\App; use think\exception\ValidateException; use think\Validate; /** * 控制器基础类 */ abstract class BaseController { use JsonTrait;//封装的API接口数据返回格式方法 use JumpTrait;//模板渲染SSR返回封装 use JwtTrait;//JWT相关封装 /** * @var string 用户类型 */ protected $scopes = ''; /** * @var bool 是否登录 */ protected $isUserLogin = false; /** * @var array 用户信息 */ protected $user = []; /** * @var string token */ protected $token = ''; /** * Request实例 * @var \think\Request */ protected $request = null; /** * 应用实例 * @var \think\App */ protected $app = null; /** * 是否批量验证 * @var bool */ protected $batchValidate = false; /** * 控制器中间件 * @var array */ protected $middleware = []; /** * @var array 应用下自动加载 vendor */ public static $vendorLoaded = []; /** * 构造方法 * BaseController constructor. */ public function __construct() { header("Content-type:text/html;charset=utf-8"); //如下先判断是因为 PluginBaseController也继承了此基类,会重写__construct,先执行了 if(!$this->app) $this->app = app(); if(!$this->request) $this->request = isset(app()->bwRequest) ? app('bwRequest') : $this->app->request; if(!$this->isUserLogin) $this->isUserLogin = isset(app()->bwRequest) ? $this->request->isUserLogin() : false; if(!$this->user) $this->user = isset(app()->bwRequest) ? $this->request->userInfo() : [];//token携带者信息 if(!$this->token) $this->token = isset(app()->bwRequest) ? $this->request->token() : ''; if(!$this->scopes) $this->scopes = isset(app()->bwRequest) ? $this->request->scopes() : ''; $appPath = app('http')->getName(); if (empty(self::$vendorLoaded[$appPath])) { $pluginVendorAutoLoadFile = app_path() .'vendor'.DS.'autoload.php'; if (file_exists($pluginVendorAutoLoadFile)) { require_once $pluginVendorAutoLoadFile; } self::$vendorLoaded[$appPath] = true; } // 控制器初始化 $this->initialize(); } // 初始化 protected function initialize() { } /** * 验证数据 * @access protected * @param array $data 数据 * @param string|array $validate 验证器名或者验证规则数组 * @param array $message 提示信息 * @param bool $batch 是否批量验证 * @return array|string|true * @throws ValidateException */ protected function validate(array $data, $validate, array $message = [], bool $batch = false) { if (is_array($validate)) { $v = new Validate(); $v->rule($validate); } else { if (strpos($validate, '.')) { // 支持场景 list($validate, $scene) = explode('.', $validate); } $class = false !== strpos($validate, '\\') ? $validate : $this->app->parseClass('validate', $validate); $v = new $class(); if (!empty($scene)) { $v->scene($scene); } } $v->message($message); // 是否批量验证 if ($batch || $this->batchValidate) { $v->batch(true); } return $v->failException(true)->check($data); } /** * 获取用户类型 * @return mixed */ public function getScopes() { return $this->scopes; } } ~~~