分析 thinkphp5 显示render不兼容问题

 2171

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

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

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

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

namespace app\lib\exception;  
   
use think\Exception;  
use think\exception\Handle;
class ExceptionHandler extends Handle  
{  
  public function render(Exception $e)  
    {  
        //TODO:
        return json('invalid request')
    }  
}

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


分析 thinkphp5 显示render不兼容问题


追踪到原始的Handle.php文件


分析 thinkphp5 显示render不兼容问题


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


分析 thinkphp5 显示render不兼容问题


修改下代码:

namespace app\lib\exception;  
   
use Exception;  
use think\exception\Handle;
class ExceptionHandler extends Handle  
{  
  public function render(Exception $e)  
    { 
        //TODO:
        return json('invalid request')
    }  
}

结果正确啦:


分析 thinkphp5 显示render不兼容问题

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

评论(0)条

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

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

    编辑推荐

    滑动的登录注册页面特效