ThinkPHP5 自定义异常处理

2023年6月18日 999+浏览

异常类似以下这种👇

那么在调试好的运行中的网站 大家一定不会想要见到这种错误吧 (用户体验在哪里)
接下来就告诉大家如何 通过配置自定义类来 跳转404错误页面

**1.关闭所有app_debug 即 'app_debug' => true,

2.在config.php中配置自定义类

'exception_handle' => '\app\common\exception\Http',

3.创建类 入下图创建文件夹及文件

4.在Http.php中写入

<?php
namespace app\common\exception;

use Exception;
use think\exception\Handle;
class Http extends Handle
{

    public function render(\Exception $e){
        if(config('app_debug')){
            //如果开启debug则正常报错
            return parent::render($e);
        }else{
            //404页面  自行定义
            header("Location:".url('/home/index/404'));
        }
    }

}

现在 可以通过关闭/开启debug来实现404/报错了。