<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 功能強大 支持多語言、二開方便! 廣告
                ## php異常托管 和 錯誤托管 跟 `php` 的 自動加載差不多。 `spl_register_autoload`: 類找不到,將調用此函數。 `set_exception_handler`: 異常,將調用此函數。 `set_error_handler`: 錯誤將調用此函數。 `session_set_save_handler`: `session` 調用此函數。 .... 分別可以說是: `類找不到托管` `異常托管` `錯誤托管` `session托管` (`自定義session` 本教程不寫) 本篇講的是: `異常托管` 和 `錯誤托管` ## 創建core/HandleExceptions.php ``` <?php namespace core; use App\exceptions\ErrorMessageException; use App\exceptions\RunErrorException; use Throwable; class HandleExceptions { // 要忽略記錄的異常 不記錄到日志去 protected $ignore = [ ]; public function init() { // 所有異常到托管到handleException方法 set_exception_handler([$this, 'handleException']); // 所有錯誤到托管到handleErorr方法 set_error_handler([$this,'handleErorr']); } // 見:https://www.runoob.com/php/php-error.html public function handleErorr($error_level, $error_message, $error_file,$error_line,$error_context) { // app函數見: "添加函數文件helpers.php" 這篇文章 app('response')->setContent( '死機 都死機 自動開機 關機 重啟再死機 三星手機 蘋果手機 所有都死機 全世界只剩小米.....' )->setCode(500)->send(); // 記錄到日志 app('log')->error( $error_message.' at '.$error_file.':'.$error_file ); } // 異常托管到這個方法 public function handleException(Throwable $e) { if( method_exists($e,'render')) // 如果自定義的異常類存在render()方法 app('response')->setContent( $e->render() )->send(); if(! $this->isIgnore($e)){ // 不忽略 記錄異常到日志去 app('log')->debug( $e->getMessage().' at '.$e->getFile().':'.$e->getLine() ); // 顯示給開發者看 以便查找錯誤 echo $e->getMessage().' at '.$e->getFile().':'.$e->getLine(); } } // 是否忽略異常 protected function isIgnore(Throwable $e) { foreach ($this->ignore as $item) if( $item == get_class($e)) return true; return false; } } ``` ## 創建app/exceptions/HandleExceptions.php 繼承基礎的異常處理類,因為 `core` 的代碼一般不給用戶改。 讓客戶去改 `app`文件夾的代碼。 ``` <?php namespace App\exceptions; use core\HandleExceptions as BaseHandleExceptions; class HandleExceptions extends BaseHandleExceptions { // 要忽略記錄的異常 不記錄到日志去 protected $ignore = [ ErrorMessageException::class ]; } ``` ## 綁定到容器 ![](https://img.kancloud.cn/34/10/3410207e1e9f883137940b9ca16bdb40_882x379.png) ## 啟動異常 錯誤托管 ![](https://img.kancloud.cn/6c/ef/6cef81cde2ecc088a7ec3cd38d9e3503_544x222.png) 至此已經完成了,接下來運行下異常。 ## 運行異常 ### 創建app/exceptions/ErrorMessageException.php ``` <?php namespace App\exceptions; use Exception; // 錯誤消息返回 class ErrorMessageException extends Exception { public function render() { return [ 'data' => $this->getMessage(), 'code' => 400 ]; } } ``` ### 取消這個異常的記錄app/exceptions/HandleExceptions.php ![](https://img.kancloud.cn/61/1a/611aa51a7fedef0a25d6f8137f4f51bb_451x380.png) ### 運行 ![](https://img.kancloud.cn/93/d7/93d7fe346f4520c2c8f514682219581b_1080x133.png) ![](https://img.kancloud.cn/43/a8/43a87edf72f3c6b9607b32b23037fcf1_780x253.png) ## 運行錯誤 ![](https://img.kancloud.cn/9b/b7/9bb7fd16d2190044eac78fe845ad400c_422x112.png) ![](https://img.kancloud.cn/83/7a/837a34e3070125535c69ce20bd7a91b8_759x237.png) ![](https://img.kancloud.cn/ef/a8/efa83bbe4e492444f09bec61ddd50734_1913x817.png)
                  <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>

                              哎呀哎呀视频在线观看