<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>

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                [TOC] ## **驗證器進階** ### **app\api\controller\v1\Banner.php** ``` namespace app\api\controller\v1; use app\api\controller\BaseController; use app\api\validate\IDMustBePositiveInt; use app\api\model\Banner as BannerModel; use app\lib\exception\MissException; class Banner extends BaseController { /** * 獲取Banner信息 * @url /banner/:id * @http get * @param int $id banner id * @return array of banner item , code 200 * @throws MissException */ public function getBanner($id) { $validate = new IDMustBePositiveInt(); $validate->goCheck(); $banner = BannerModel::getBannerById($id); if (!$banner ) { throw new MissException([ 'msg' => '請求banner不存在', 'errorCode' => 40000 ]); } return $banner; } } ``` ## **app\api\validate\IDMustBePositiveInt.php** ``` namespace app\api\validate; class IDMustBePositiveInt extends BaseValidate { protected $rule = [ 'id' => 'require|isPositiveInteger', ]; } ``` ## **app\api\validate\BaseValidate.php** ``` namespace app\api\validate; use app\lib\exception\ParameterException; use think\Request; use think\Validate; /** * 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; } protected function isPositiveInteger($value, $rule='', $data='', $field='') { if (is_numeric($value) && is_int($value + 0) && ($value + 0) > 0) { return true; } return $field . '必須是正整數'; } } ``` ## **錯誤處理相關代碼** 在`app`目錄下面的`provider.php`文件中綁定異常處理類,例如: 替換原有的vendor\\topthink\\framework\\src\\think\\exception\\Handle.php的Handle類 ~~~ // 綁定自定義異常處理handle類 'think\exception\Handle' => '\\app\\common\\exception\\Http', 'exception_handle' => '\app\lib\exception\ExceptionHandler',//這是tp5的config.php的配置 ~~~ >[danger] 事實上,默認安裝應用后,已經幫你內置了一個公共的`app\ExceptionHandle`異常處理類(作用于index、admin、api等所有模塊)它繼承至默認的handle類,直接修改該類的相關方法即可完成應用的自定義異常處理機制。而無需在自定義Handle類 ### **app\lib\exception\ExceptionHandler.php** ``` <?php namespace app\lib\exception; use think\exception\Handle; use think\Log; use think\Request; use Exception; /* * 重寫Handle的render方法,實現自定義異常消息 */ class ExceptionHandler extends Handle { private $code; private $msg; private $errorCode; public function render(Exception $e) { if ($e instanceof BaseException) { //如果是自定義異常,則控制http狀態碼,不需要記錄日志 //因為這些通常是因為客戶端傳遞參數錯誤或者是用戶請求造成的異常 //不應當記錄日志 $this->code = $e->code; $this->msg = $e->msg; $this->errorCode = $e->errorCode; } else{ // 如果是服務器未處理的異常,將http狀態碼設置為500,并記錄日志 if(config('app_debug')){ // 調試狀態下需要顯示TP默認的異常頁面,因為TP的默認頁面 // 很容易看出問題 return parent::render($e); } $this->code = 500; $this->msg = 'sorry,we make a mistake. (^o^)Y'; $this->errorCode = 999; $this->recordErrorLog($e); } $request = Request::instance(); $result = [ 'msg' => $this->msg, 'error_code' => $this->errorCode, 'request_url' => $request = $request->url() ]; return json($result, $this->code); } /* * 將異常寫入日志 */ private function recordErrorLog(Exception $e) { Log::init([ 'type' => 'File', 'path' => LOG_PATH, 'level' => ['error'] ]); // Log::record($e->getTraceAsString()); Log::record($e->getMessage(),'error'); } } ``` ### **app\lib\exception\BaseException.php** ``` <?php namespace app\lib\exception; use think\Exception; /** * Class BaseException * 自定義異常類的基類 */ class BaseException extends Exception { public $code = 400; public $msg = 'invalid parameters'; public $errorCode = 999; public $shouldToClient = true; /** * 構造函數,接收一個關聯數組 * @param array $params 關聯數組只應包含code、msg和errorCode,且不應該是空值 */ public function __construct($params=[]) { if(!is_array($params)){ return; } if(array_key_exists('code',$params)){ $this->code = $params['code']; } if(array_key_exists('msg',$params)){ $this->msg = $params['msg']; } if(array_key_exists('errorCode',$params)){ $this->errorCode = $params['errorCode']; } } } ``` ### **app\lib\exception\ParameterException.php** ``` <?php namespace app\lib\exception; /** * Class ParameterException * 通用參數類異常錯誤 */ class ParameterException extends BaseException { public $code = 400; public $errorCode = 10000; public $msg = "invalid parameters"; } ```
                  <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>

                              哎呀哎呀视频在线观看