本文小编为大家详细介绍“thinkphp5显示render不兼容怎么解决”,内容详细,步骤清晰,细节处理妥当,希望这篇“thinkphp5显示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不兼容怎么解决

读到这里,这篇“thinkphp5显示render不兼容怎么解决”文章已经介绍完毕,想要掌握这篇文章的知识点还需要大家自己动手实践使用过才能领会,如果想了解更多相关内容的文章,欢迎关注***行业资讯频道。

相关文章:

  • 2021-05-23
  • 2022-12-23
  • 2022-01-02
  • 2022-12-23
  • 2021-05-27
  • 2022-12-23
  • 2022-12-23
  • 2021-04-17
猜你喜欢
  • 2021-08-13
  • 2022-12-23
  • 2023-03-21
  • 2021-06-17
  • 2021-12-20
相关资源
相似解决方案