## 驗證器的位置

繼承**BaseValidate**驗證器
```
~~~
/**
* Class BaseValidate
* 驗證類的基類
*/
class BaseValidate extends Validate
{
/**
* 檢測所有客戶端發來的參數是否符合驗證類規則
* 基類定義了很多自定義驗證方法
* 這些自定義驗證方法其實,也可以直接調用
* @throws ParameterException
* @return true
*/
public function goCheck()
{
//必須設置contetn-type:application/json
$request = Request::instance();
$params = $request->param();
$params['token'] = $request->header('token');
if (!$this->check($params)) {
$exception = new ParameterException(
[
// $this->error有一個問題,并不是一定返回數組,需要判斷
'msg' => is_array($this->error) ? implode(
';', $this->error) : $this->error,
]);
throw $exception;
}
return true;
}
/**
* @param array $arrays 通常傳入request.post變量數組
* @return array 按照規則key過濾后的變量數組
* @throws ParameterException
*/
public function getDataByRule($arrays)
{
if (array_key_exists('user_id', $arrays) | array_key_exists('uid', $arrays)) {
// 不允許包含user_id或者uid,防止惡意覆蓋user_id外鍵
throw new ParameterException([
'msg' => '參數中包含有非法的參數名user_id或者uid'
]);
}
$newArray = [];
foreach ($this->rule as $key => $value) {
$newArray[$key] = $arrays[$key];
}
return $newArray;
}
protected function isPositiveInteger($value, $rule='', $data='', $field='')
{
if (is_numeric($value) && is_int($value + 0) && ($value + 0) > 0) {
return true;
}
return $field . '必須是正整數';
}
protected function isNotEmpty($value, $rule='', $data='', $field='')
{
if (empty($value)) {
return $field . '不允許為空';
} else {
return true;
}
}
//沒有使用TP的正則驗證,集中在一處方便以后修改
//不推薦使用正則,因為復用性太差
//手機號的驗證規則
protected function isMobile($value)
{
$rule = '^1(3|4|5|7|8)[0-9]\d{8}$^';
$result = preg_match($rule, $value);
if ($result) {
return true;
} else {
return false;
}
}
// // 令牌合法并不代表操作也合法
// // 需要驗證一致性
// protected function isUserConsistency($value, $rule, $data, $field)
// {
// $identities = getCurrentIdentity(['uid', 'power']);
// extract($identities);
//
// // 如果當前令牌是管理員令牌,則允許令牌UID和操作UID不同
// if ($power == ScopeEnum::Super) {
// return true;
// }
// else {
// if ($value == $uid) {
// return true;
// }
// else {
// throw new TokenException([
// 'msg' => '你怎么可以用自己的令牌操作別人的數據?',
// 'code' => 403,
// 'errorCode' => '10003'
// ]);
// }
// }
// }
}
~~~
```
## 其次在 其他驗證器里面就繼承就行了 如::
~~~
class TokenGet extends BaseValidate
{
protected $rule = [
'code' => 'require|isNotEmpty'
];
protected $message=[
'code' => '沒有code還想拿token?做夢哦'
];
}
~~~
或者
```
~~~
class AddressNew extends BaseValidate
{
// 為防止欺騙重寫user_id外鍵
// rule中嚴禁使用user_id
// 獲取post參數時過濾掉user_id
// 所有數據庫和user關聯的外鍵統一使用user_id,而不要使用uid
protected $rule = [
'name' => 'require|isNotEmpty',
'mobile' => 'require|isMobile',
'province' => 'require|isNotEmpty',
'city' => 'require|isNotEmpty',
'country' => 'require|isNotEmpty',
'detail' => 'require|isNotEmpty',
];
}
~~~
```
## 控制器里面調用
```
~~~
$validate = new IDMustBePositiveInt();
$validate->goCheck();
~~~
```
- tp5圖片上傳
- 文件上傳到七牛云
- 上傳到阿里云
- 富文本編輯器
- phpexcel和spreadsheet
- phpexcel導出
- phpexcel導入
- spreadsheet
- tp5_api接口
- 跨域請求
- JWT
- 圖片和視頻上傳接口
- 驗證碼
- tp5小程序登錄
- tp5小程序支付
- tp5基礎架構
- 驗證層
- 模型層Model
- 控制器構找
- tp5.0支付寶
- 海報二維碼
- 輪播圖
- echarts柱狀圖
- layui的圖片彈窗
- p標簽顯示指定行數(全部)
- jquery和layerdate調用日期
- ajax發送文件和圖片的坑啊
- JS日期點擊上一天和下一天
- 百度分享js
- POST請求
- 商品數據表
- tp5.0支付寶最全
- tp5路由的坑
- 二維數組排序
- tp5模型分組group錯誤
- 二維變一維數組
- 無限樹形結構
- json對象轉數組
- 模型關聯查詢
- tp5的模型獲取器和字段設定
- 經緯度獲取距離排序