# tp5.1筆記
>[info] 實踐記錄
1. 用facade替換類實例化操作,代碼簡潔、清淅易于維護
2. app()統一操作,代碼清淅簡潔
3. 數據處理統一定義于model中,比如數據緩存,數據轉換,拼接
4. 日志可利用注入、行為、控制器前后置操作來執行記錄方法
5. 定義DS簡化代碼 `define('DS', DIRECTORY\_SEPARATOR)`
6. 善用定義命令行console減少重復工作
7. 前端組件集成,可參照fastadmin,dolphinPHP中的集成方法,可提高開發效率,
8. >[info] 使用方法,可通過 thinkphp\helper.php 自帶助手函數,學習內置類調用方法
9. 善用traits減少重復代碼
10. 控制器: 前置操作,方便臨時增加的處理過程; 當然中間插件也是不錯的選擇
11. 模型:數據轉換,緩存.....與數據相關的,統一放于模型之中
12.
# 加載與配置
~~~
use think\facace\Load;
use think\facace\Config
include app('env')\->get('app\_path').'chsys'.DIRECTORY\_SEPARATOR.'common.php';
~~~
* 只在部分控制器添加特定配置
~~~
.....
class Index extends Admin
{
//初始化 左菜單
public function _initialize(){
parent::_initialize();
\think\facade\Config::set('my',....);
}
initialize
~~~
# 控制器 Route,Controller
\think\facace\Route;
\think\Controller;
* 只在部分控制器中,加啟用配置或者添加路由方法
>[info] 可通過行為【tags.php]添加 或者 intiallize()中動態加載
~~~
class Linktest extends Admin
{
//初始化 左菜單
public function _initialize(){
parent::_initialize();
Route::
// dump((new CustomerM)->quickAdd());
}
~~~
# 數據處理 Db,Model
----
~~~
\think\Db;
\think\Model;
hasOne('關聯模型名','外鍵名','主鍵名',['模型別名定義'],'join類型');
~~~
## 實用數據查詢方法
* 默認模型返回為數據集格式數據 轉換為數組方法 用函數( `collection()` )
~~~
collection(CustomerM::group('name')->field('id,name')->select())->toArray();
~~~
* 單條記錄返回數據方法
~~~
CustomerM::find(1)->toArray();
CustomerM::find(1)->toArray();
~~~
* 獲取指定字段所有數據方法
~~~
CustomerM::where('name','like','%name%')->column('id,name');
//返回格式為
[
'$id'=>"$name",
'$id'=>"$name"
];
~~~
* 返回指定字段的值 【單條記錄】
~~~
CustomerM::where('id',5)->value('name');
//或者
CustomerM::where('id',5)->getFieldName();
~~~
# 驗證
* 內置支持驗證規則
~~~~
// 驗證規則默認提示信息
protected static $typeMsg = [
'require' => ':attribute不能為空',
'number' => ':attribute必須是數字',
'integer' => ':attribute必須是整數',
'float' => ':attribute必須是浮點數',
'boolean' => ':attribute必須是布爾值',
'email' => ':attribute格式不符',
'array' => ':attribute必須是數組',
'accepted' => ':attribute必須是yes、on或者1',
'date' => ':attribute格式不符合',
'file' => ':attribute不是有效的上傳文件',
'image' => ':attribute不是有效的圖像文件',
'alpha' => ':attribute只能是字母',
'alphaNum' => ':attribute只能是字母和數字',
'alphaDash' => ':attribute只能是字母、數字和下劃線_及破折號-',
'activeUrl' => ':attribute不是有效的域名或者IP',
'chs' => ':attribute只能是漢字',
'chsAlpha' => ':attribute只能是漢字、字母',
'chsAlphaNum' => ':attribute只能是漢字、字母和數字',
'chsDash' => ':attribute只能是漢字、字母、數字和下劃線_及破折號-',
'url' => ':attribute不是有效的URL地址',
'ip' => ':attribute不是有效的IP地址',
'dateFormat' => ':attribute必須使用日期格式 :rule',
'in' => ':attribute必須在 :rule 范圍內',
'notIn' => ':attribute不能在 :rule 范圍內',
'between' => ':attribute只能在 :1 - :2 之間',
'notBetween' => ':attribute不能在 :1 - :2 之間',
'length' => ':attribute長度不符合要求 :rule',
'max' => ':attribute長度不能超過 :rule',
'min' => ':attribute長度不能小于 :rule',
'after' => ':attribute日期不能小于 :rule',
'before' => ':attribute日期不能超過 :rule',
'expire' => '不在有效期內 :rule',
'allowIp' => '不允許的IP訪問',
'denyIp' => '禁止的IP訪問',
'confirm' => ':attribute和確認字段:2不一致',
'different' => ':attribute和比較字段:2不能相同',
'egt' => ':attribute必須大于等于 :rule',
'gt' => ':attribute必須大于 :rule',
'elt' => ':attribute必須小于等于 :rule',
'lt' => ':attribute必須小于 :rule',
'eq' => ':attribute必須等于 :rule',
'unique' => ':attribute已存在',
'regex' => ':attribute不符合指定規則',
'method' => '無效的請求類型',
'token' => '令牌數據無效',
'fileSize' => '上傳文件大小不符',
'fileExt' => '上傳文件后綴不符',
'fileMime' => '上傳文件類型不符',
];
~~~~
- 文檔說明
- 5.1對比5.0.X版本需要注意點
- 待解答問題QA
- THINKPHP基礎
- 常用技巧
- tp5.1系統信息
- 容器、Facade、依賴注入
- 其它要點記錄
- 數據庫與模型
- 數據集
- AQ問題集
- API開發
- restful開發
- restful測試之ZClient開發
- api開發
- 緩存
- 組件開發
- 采集組件ZSnoopy
- restful測試組件ZClient
- thinkphp各功能模塊
- thinkphp-Log
- 隊列thinkphp-Queue
- THINKPHP擴展
- 單元測試
- 類庫庫遷移工具Migration
- 命令行
- 關于console擴展要點
- 附錄
- 第三方實用PHP庫
- 實用IP庫
- phpQuery庫
- Guzzle庫
- Markdown解釋庫Parsedown
- 上線的項目debug組件
- nette/utils實用函數庫
- 推薦框架DolphinPHP
- Thinkphp之widget
- 表單組件form
- 前端框架推薦
- 推薦框架fastadmin
- PHP7最新語法
- 前端框架
- Echart
- mysql觸發器
- PHP實用技巧與函數
- composer實用筆記