慕课网 微信小程序商城构建全栈应用 tp5【总结】

myphp.vip

1.异常处理:

【代码越抽象,复用性越高】

【封装性越好,适应代码变化的能力越强】

慕课网 微信小程序商城构建全栈应用  tp5【总结】

【】

<?php
/**
* Created by PhpStorm.
* User: 14155
* Date: 2018/12/22
* Time: 23:40
*/ namespace app\lib\exception; use think\Exception;
use think\exception\Handle;
use think\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 {
$this->code = 500;
$this->msg = '服务器内部错误';
$this->errorCode = 999;
}
$request = Request::instance();
$result = [
'msg' => $this->msg,
'error_code' => $this->errorCode,
'request_url' => $request->url()
];
return json($result, $this->code);
}
}

【】

<?php
/**
* Created by PhpStorm.
* User: 14155
* Date: 2018/11/10
* Time: 0:31
*/ namespace app\lib\exception; use think\Exception; class BaseException extends Exception
{
// HTTP 状态码 404,200
public $code = 400; // 错误具体信息(英文)
public $msg = 'Parameter error'; // 自定义的错误码
public $errorCode = 10000; }

【】

<?php
/**
* Created by PhpStorm.
* User: 14155
* Date: 2018/12/22
* Time: 23:44
*/ namespace app\lib\exception; class BannerMissException extends BaseException
{
public $code = 404;
//请求的banner不存在;
public $msg = 'The request Banner does not exist';
public $errorCode = 40000;
}

【】

上一篇:Treemap 有序的hashmap。用于排序


下一篇:[z]Google SPDY介绍