<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??一站式輕松地調用各大LLM模型接口,支持GPT4、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                ![](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:創建更多的自定義驗證類 階段六:自定義驗證類文件多了,就需要工廠類來封裝啦!
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看