🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
![](https://img.kancloud.cn/d5/f2/d5f21e591e656fcd469834ef0af5dea3_245x282.png) ## **阶段1:基础** **application/controller/v1/Banner.php** ~~~ <?php namespace app\api\controller\v1; use think\Controller; use think\validate; class Banner extends controller{ public function index(){ //http://localhost/thinkphp5/public/index.php/api/v1.Banner/index } public function getBanner(){ //待验证数据 $data=array( 'name'=>'dash', 'email'=>'wolichihua2011@163.com' ); //独立验证 $validate=new Validate([ 'name'=>'require|max:10', 'email'=>'email' ]); //batch()批量验证 否则只返回最后一个getError验证信息 $result=$validate->batch()->check($data);//返回布尔 var_dump($result); var_dump($validate->getError());//返回错误信息 } } ~~~ ### **阶段二:将验证规则单独放到其他的类文件中** **application/api/validate/TestValidate.php** ~~~ <?php namespace app\api\validate; use think\Validate; class TestValidate extends Validate{ protected $rule =[ 'name'=>'require|max:10', 'email'=>'email' ]; } ~~~ **application/controller/v1/Banner.php** ~~~ <?php namespace app\api\controller\v1; use think\Controller; use think\validate; //use app\api\validate\TestValidate; class Banner extends controller{ public function index(){ //http://localhost/thinkphp5/public/index.php/api/v1.Banner/index } public function getBanner($id){ $data=array( 'name'=>'dash', 'email'=>'wolichihua2011@163.com' ); //验证器 直接new $validate= new \app\api\validate\TestValidate(); //或者引入命名空间在new use app\api\validate\TestValidate (application/api/validate/TestValidate.php)    $validate= new TestValidate();//必须有这个命名空间 use app\api\validate\TestValidate //batch()批量验证 否则只返回最后一个getError验证信息 $result=$validate->batch()->check($data);//返回布尔 var_dump($result); var_dump($validate->getError());//返回错误信息 } } ~~~ ### **阶段三:封装验证参数:** 创建一个应用验证器基类,只定义对外的一个对外开放的goCheck入口和自定义验证规则,其他功能由子类负责 **application/api/validate/BaseValidate.php** ~~~ <?php namespace app\api\validate; use think\Request; use think\Validate; use think\Exception; class BaseValidate extends Validate{ public function goCheck(){ // 获取http参数 // 对这些参数做检验 $request= Request::instance(); $params=$request->param(); $result=$this->check($params); if (!$result) { $error=$this->error; throw new Exception($error, 1); }else{ return true; } } } ~~~ **application/api/validate/IDMustBePositiveInt.php** ~~~ <?php namespace app\api\validate; class IDMustBePositiveInt extends BaseValidate{ protected $rule=array( 'id'=>'require|isPositiveInteger' ); //自定义验证规则(请移至BaseValidate.php中,方便其他验证器类使用) protected function isPositiveInteger($value, $rule='', $data='', $field='') { if (is_numeric($value) && is_int($value + 0) && ($value + 0) > 0) { return true; } return $field . '必须是正整数'; } } ~~~ 订单验证规则 子类独立验证器同时验证多个字段时 ``` namespace app\api\validate; use app\api\validate\BaseValidate; use app\lib\exception\ParameterException; class OrderPlace extends BaseValidate { protected $rule=[ 'products'=>'checkProducts' ]; protected function checkProducts($values) { if(empty($values)){ throw new ParameterException(['msg'=>'商品列表不能为空']); } if(!is_array($values)){ throw new ParameterException(['msg'=>'商品参数不正确']); } foreach ($values as $key => $value) { $this->checkProduct($value); } return true; } /* $values的格式如下 $products=[ ['product_id'=>1,['count'=>3]], ['product_id'=>2,['count'=>3]], ['product_id'=>3,['count'=>3]], ['product_id'=>4,['count'=>3]], ['product_id'=>5,['count'=>3]], ]; */ //注意要将IDMustBePositiveInt.php类的自定义规则isPositiveInteger方法移至BaseValidate.php时才能在下面的规则中使用 protected $singleRule=[ 'product_id'=>'require|isPositiveInteger', 'count'=>'require|isPositiveInteger', ]; protected function checkProduct($value) { $validate=new BaseValidate($this->singleRule); $result=$validate->check($value); if(!$result){ throw new ParameterException(['msg'=>'商品列表参数错误']); } } } ```  **application/controller/v1/Banner.php** ~~~ <?php namespace app\api\controller\v1; use think\Controller; use think\validate; use app\api\validate\IDMustBePositiveInt; class Banner extends controller{ public function index(){ http://localhost/thinkphp5/public/index.php/api/v1.Banner/index } public function getBanner($id){ (new IDMustBePositiveInt())->goCheck(); } public function getOrders(){ (new OrderPlace())->goCheck(); //... } } ~~~ 阶段4:将自定义规则挪到BaseValidate.php中,其他自定义的也一样只保留rule规则就行了 阶段5:创建更多的自定义验证类 阶段六:自定义验证类文件多了,就需要工厂类来封装啦!