thinkphp5 Exception类重定义

时间:2023-03-09 01:55:04
thinkphp5  Exception类重定义

重点定义自己的错误信息和错误码;

在TP5的配置文件中有下面一段

// 异常处理handle类 留空使用 \think\exception\Handle

'exception_handle'       => '',
指错误信息来自自于\think\exception\Handle的方法里;那么可以重新定义一个类,再重写这个hander方法即可;
======================================
1.定义config:
'exception_handle'       => 'app\lib\exception\ExceptionHandler',
2.在app\lib\exception\ExceptionHander.php中:
class ExceptionHandler extends Handle
{
public function render(Exception $e)
{
return json('这里是自定义的错误');
//return parent::render($e); // TODO: Change the autogenerated stub }
}