多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
全局异常处理handler ~~~ public function render(\Exception $e) { if($e instanceof BaseException){ // 自定义的异常 $this->code=$e->code; $this->msg=$e->msg; $this->errCode=$e->errCode; }else{ // 系统的异常,判断是不是调试模式:是,显示tp5的异常,否则显示封装接口的异常 if(config('app.app_debug')){ //使用tp5默认的错误提示 return parent::render($e); }else{ $this->code=500; $this->msg='服务器内部错误'; $this->errCode=999; //真是的错误写入日志 // echo($e->getMessage()); Log::record($e->getMessage(),'error'); } } //返回错误数据 $url=request()->url(); $result=[ 'msg'=>$this->msg, 'errCode'=>$this->errCode, 'url'=>$url ]; return json($result,$this->code); ~~~ 自定义异常类 ~~~ class BaseException extends Exception { //HTTP 状态码 404,200 public $code=400; //错误具体信息 public $msg="自定义错误"; //自定义的错误码 public $errCode=10000; //构造函数初始化类属性 public function __construct(array $conf=[]) { //防御性代码 if(!is_array($conf)) return; //属性初始化 array_key_exists('msg',$conf)&&$this->msg=$conf['msg']; array_key_exists('errCode',$conf)&&$this->errCode=$conf['errCode']; array_key_exists('code',$conf)&&$this->url=$conf['code']; } } ~~~