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

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                參數請求驗證自定義 寫一個關于基礎類的驗證方法 BaseValidate.php ``` <?php namespace app\api\validate; use think\Exception; use think\Request; use think\Validate; 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); }else{ // echo '111'; return true; } } } ``` 對一個傳入$id 進行驗證 IdValidate.php ``` <?php namespace app\api\validate; use \app\api\validate\BaseValidate; class IdValidate extends BaseValidate { protected $rule = [ 'id' =>"require|int" ]; } ``` 在控制器中進行使用 ``` (new IdValidate())->goCheck(); ``` 異常錯誤自定義 創建一個異常目錄 exception 創建第一個基礎類異常文件BaseException.php 此文件定義HTTP狀態碼 錯誤信息 以及自定義錯誤碼 ``` <?php namespace app\lib\exception; use think\Exception; class BaseException extends Exception { //HTTP 狀態碼 404 200 public $code = 400; //錯誤信息 public $msg = '參數錯誤'; //自定義的錯誤碼 public $errorcode = 10000; } ``` 創建文章請求異常文件 ArticleMissException.php ``` <?php namespace app\lib\exception; class ArticleMissException extends BaseException { public $code = 404; public $msg = '請求的文章不存在'; public $errorcode = 40000; } ``` 修改(TP5的配置文件config.php搜:exception_handle): // 異常處理handle類 留空使用 \think\exception\Handle ``` // 異常處理handle類 留空使用 \think\exception\Handle 'exception_handle' => 'app\lib\exception\ExceptionHandler', ``` 異常處理文件 ExceptionHandler .php ``` <?php namespace app\lib\exception; use Exception; use think\exception\Handle; use app\lib\exception\BaseException; use think\Request; class ExceptionHandler extends Handle { private $code; private $msg; private $errorcode; //需要返回當前客戶的URL路徑 public function render(Exception $e){ if($e instanceof BaseException){ //如果是自定義異常 $this->code = $e->code; $this->msg = $e->msg; $this->errorcode = $e->errorcode; }else{ $this->code = 500; $this->msg = '服務器內部錯誤'; $this->errorcode = 999; $this->recordErrorLog($e); //記錄到日記中 } $request = Request::instance(); $result = [ 'msg'=>$this->msg, 'error_code'=>$this->errorcode, 'request_url'=>$request->url() ]; return json($result,$this->code); } /** * 未知錯誤寫入日記 * @param Exception $e * 1、初始化TP5日記 * 2、Log::record() 記錄日志信息到內存 * $e->getMessage() 日記信息 * 'error' 日記級別 */ private function recordErrorLog(Exception $e){ Log::init([ 'type'=>'File', 'path'=>APP_PATH.'lib/exception/log/', 'level'=>['error'] ]); Log::record($e->getMessage(),'error'); } } ``` 上面ExceptionHandler .php 第一個 use Exception 原因: handle下的render方法里(Exception $e)用的是use Exception,不能使用 use think\Exception; 將use think\exception 改為 use Exception 驗證異常: 模型頁面 Article.php ``` <?php namespace app\api\model; use think\Model; class Article extends Model { public static function getArticleByID($id){ // TODO:根據文章 ID 獲取文章 信息 return null; } } ``` 控制器頁面 Article.php ``` <?php namespace app\api\controller\v1; use app\api\validate\IdValidate; use app\api\model\Article as ArticleModel; use app\lib\exception\ArticleMissException; class Article { /*** * 獲取指定id的文章信息 * @url /Article/:id * @http GET * @id Article的id */ public function getArticle($id){ (new IdValidate())->goCheck(); $artcile= ArticleModel::getArticleByID($id); if(!$artcile){ //throw new Exception('內部錯誤'); //測試日記內部錯誤 throw new ArticleMissException(); } return $artcile; } } ```
                  <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>

                              哎呀哎呀视频在线观看