最近闲着无事,在家学thinkphp开发小程序,感觉里面有个对thinkphp异常处理类写得非常的方便,特记录下来。
首先我们需要在thinkphp的配置文件app.php中配置自定义的异常处理类地址。
'exception_handle' => 'app\lib\exception\ExceptionHandler',
然后书写我们自己的异常处理类
<?php
/**
*ExceptionHandler.php
*Create on 2020/3/4 3:42 下午
*Create by lxllsy520
*/
namespace app\lib\exception;
use Exception;
use think\exception\Handle;
use think\facade\Log;
use think\facade\Request;
class ExceptionHandler extends Handle
{
private $code;
private $msg;
private $errorCode;
//需要返回客户端当前请求的URL路径
public function render(Exception $e)
{
if ($e instanceof BaseException){
//如果是自定义的异常则正常浏览器抛出
$this->code = $e->code;
$this->msg = $e->msg;
$this->errorCode = $e->errorCode;
}else{
//如果是调试模式就使用tp自带的异常提示,如果是生产模式就使