tp6 统一异常处理类书写

1、common.php公共方法文件中新建方法returnCode()

function returnMsg($status,$message,$data=array()){
    $result =array(
        'status'=>$status,
        'message'=>$message,
        'data'=>$data,
    );
    return json($result);
}

2、修改app/provide.php文件为:

<?php
use app\ExceptionHandle;

return [
    'think\exception\Handle' => '\\app\\common\\HttpException',
];

3、app下新建common模块,在common模块下新建   HttpException.php文件

<?php

namespace app\common;

use think\exception\Handle;
use think\Response;
use Throwable;

class HttpException extends Handle
{
    public $httpCode = 500;

    public function render($request, Throwable $e): Response
    {
        if (method_exists($e,"getStatusCode")){
            $httpCode = $e->getStatusCode();
        }else{
            $httpCode = $this->httpCode;
        }
        return returnMsg($httpCode, $e->getMessage(),[]);
    }
}

4、做个问题测试即可

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值