分析 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')     }   }

之后出现postman检验接口出现如下错误提示不兼容:
分析 thinkphp5 显示render不兼容问题

追踪到原始的Handle.php文件,
分析 thinkphp5 显示render不兼容问题

查看下use,发现源文件用的是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 分享