分析 thinkphp5 顯示render不兼容問題

下面thinkphp框架教程欄目將給大家介紹關于tp5自定義全局異常處理提示render不兼容問題,希望對需要的朋友有所幫助!

TP5自定義全局異常處理,所有拋出的異常都通過自定義render方法渲染,再返回客戶端顯示。
需要自定義handle的render方法并覆蓋:

namespace applibexception;      use thinkException;   use thinkexceptionHandle; class ExceptionHandler extends Handle   {     public function render(Exception $e)       {           //TODO:         return json('invalid request')     }   }

之后出現(xiàn)postman檢驗接口出現(xiàn)如下錯誤提示不兼容:
分析 thinkphp5 顯示render不兼容問題

追蹤到原始的Handle.php文件,
分析 thinkphp5 顯示render不兼容問題

查看下use,發(fā)現(xiàn)源文件用的是Exception,而我用的thinkException:
分析 thinkphp5 顯示render不兼容問題

立即學習PHP免費學習筆記(深入)”;

修改下代碼:

namespace applibexception;      use Exception;   use thinkexceptionHandle; class ExceptionHandler extends Handle   {     public function render(Exception $e)       {          //TODO:         return json('invalid request')     }   }

結果正確啦:
分析 thinkphp5 顯示render不兼容問題

推薦:《最新的10個thinkphp視頻教程》

? 版權聲明
THE END
喜歡就支持一下吧
點贊11 分享