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

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                ### **安裝TP6** ~~~ composer create-project topthink/think tp && cd tp ~~~ 開啟TP6多應用模式 ~~~ composer require topthink/think-multi-app ~~~ 安裝thinkphp模板引擎 ~~~ composer require topthink/think-view ~~~ 安裝驗證碼擴展 ~~~ composer require topthink/think-captcha ~~~ ### **自定義全局異常處理類** 在\\extend目錄下新建擴展exception,自定義ExceptionHandler繼承TP的think\\exception\\Handle類,在ExceptionHandler類中重寫render方法 ~~~ public function render($request, Throwable $e): Response { if ($e instanceof BaseException) { // 自定義異常走的邏輯 $this->code = $e->getCode(); $this->message = $e->getMessage(); $this->errorCode = $e->getErrorCode(); } else { // 服務器異常邏輯,不需要將具體錯誤信息展示給用戶 $this -> code = 500; $this -> message = '服務器內部錯誤'; $this -> errorCode = 999;      // 此處要記錄日志,留待之后添加 } $result = [ 'message' => $this->message, 'errorCode' => $this->errorCode, 'request_url' => Request::url(), ]; return json($result, $this->code); } ~~~ 使自定義全局異常生效: app目錄下的provider.php中定義容器: ~~~ 'think\exception\Handle' => \exception\ExceptionHandler::class, ~~~ 定義BaseException繼承think\\Exception,定義存儲錯誤信息的成員變量 ~~~ // HTTP 狀態碼 protected $code = 400; // 錯誤具體信息 protected $message = '參數錯誤'; // 自定義錯誤碼 protected $errorCode = 10000;   ~~~ 重寫\_\_construct()方法,讓自定義異常處理類接收一個數組參數 ~~~ public function __construct($params = []) { if (!is_array($params)){ throw new Exception('參數必須是數組'); } if (array_key_exists('code',$params)){ $this -> code = $params['code']; } if (array_key_exists('message',$params)){ $this -> message = $params['message']; } if (array_key_exists('errorCode',$params)){ $this -> errorCode = $params['errorCode']; } } ~~~ ### **構建驗證層** 在validate目錄下定義BaseValidate繼承think\\Validate,實現gocheck方法 ~~~ public function goCheck(){ $params = Request::param(); $result = $this -> check($params); if (!$result){ throw new \exception\ParamsException([ 'message'=> $this -> error, 'code' => 400 ]); }else{ return true; } } ~~~ 其他的validate文件繼承BaseValidate,如LoginCheck繼承BaseValidate 在控制器里就可以通過?(newLoginCheck()) ->goCheck();?實現參數校驗 校驗不通過則拋出自定義異常
                  <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>

                              哎呀哎呀视频在线观看