分析 thinkphp5 显示render不兼容问题

 3618

下面给大家介绍关于TP5自定义全局异常处理提示render不兼容问题,希望对需要的朋友有所帮助!

【填坑】TP5自定义全局异常处理提示render不兼容

TP5自定义全局异常处理,所有抛出的异常都通过自定义render方法渲染,再返回客户端显示。

需要自定义handle的render方法并覆盖:

  1. namespace app\lib\exception;  
  2.    
  3. use think\Exception;  
  4. use think\exception\Handle;
  5. class ExceptionHandler extends Handle  
  6. {  
  7.   public function render(Exception $e)  
  8.     {  
  9.         //TODO:
  10.         return json('invalid request')
  11.     }  
  12. }

之后出现postman检验接口出现如下错误提示不兼容:


分析 thinkphp5 显示render不兼容问题


追踪到原始的Handle.php文件


分析 thinkphp5 显示render不兼容问题


查看下use,发现源文件用的是Exception,而我用的think\Exception


分析 thinkphp5 显示render不兼容问题


修改下代码:

  1. namespace app\lib\exception;  
  2.    
  3. use Exception;  
  4. use think\exception\Handle;
  5. class ExceptionHandler extends Handle  
  6. {  
  7.   public function render(Exception $e)  
  8.     { 
  9.         //TODO:
  10.         return json('invalid request')
  11.     }  
  12. }

结果正确啦:


分析 thinkphp5 显示render不兼容问题

本文网址:https://www.zztuku.com/detail-10236.html
站长图库 - 分析 thinkphp5 显示render不兼容问题
申明:如有侵犯,请 联系我们 删除。

评论(0)条

您还没有登录,请 登录 后发表评论!

提示:请勿发布广告垃圾评论,否则封号处理!!

    编辑推荐