下面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)如下錯誤提示不兼容:
追蹤到原始的Handle.php文件,
查看下use,發(fā)現(xiàn)源文件用的是Exception,而我用的thinkException:
立即學習“PHP免費學習筆記(深入)”;
修改下代碼:
namespace applibexception; use Exception; use thinkexceptionHandle; class ExceptionHandler extends Handle { public function render(Exception $e) { //TODO: return json('invalid request') } }
結果正確啦:
推薦:《最新的10個thinkphp視頻教程》
? 版權聲明
文章版權歸作者所有,未經(jīng)允許請勿轉載。
THE END