本文小编为大家详细介绍“thinkphp5显示render不兼容怎么解决”,内容详细,步骤清晰,细节处理妥当,希望这篇“thinkphp5显示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检验接口出现如下错误提示不兼容:
追踪到原始的Handle.php文件,
查看下use,发现源文件用的是
Exception,而我用的
thinkException:
修改下代码:
namespace applibexception;  
  
use Exception;  
use thinkexceptionHandle;
class ExceptionHandler extends Handle  
{  
  public function render(Exception $e)  
    { 
        //TODO:
        return json('invalid request')
    }  
}结果正确啦: