多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
errorContainer是什么? 这个控制器用在哪儿合适? errorContainer是个单例,所以,就等于是一个内存缓存。从index业务开始,不管中间是调用了logic的还是model的还是其他的controller的控制器,errorContainer中的数据都是同步一样的。直到此用户请求结束,进程结束。 errorContainer主要是为业务方法只返回true|false结果 当返回为true时代表执行正确。 当返回为false时,执行错误,在controller中想知道具体错误则调用 errorContainer->getLastCode() 或者getLastMsg() 或者 getAllError ----------------- DEMO: 在controller中: ~~~ $errorObj = errorContainer::instance(); $auth = new userAuthentic($planId, $this->uid, $pic); // 检查 根据配置找到对应的方法 是否存在 if(!$auth->auth()){ failReturn($errorObj->getLastCode(), $errorObj->getLastMsg()); } ~~~ 在userAuthentic中: ~~~ public function auth() { if(empty($this->info['realName'])){ errorContainer::instance()->setError('cajl003', '缺少真实姓名'); return false; } // 中间省略一些代码 if($faceRet > 0.747){ return true; }else{ errorContainer::instance()->setError('cajl005', '人脸对比失败,分值过低'); return false; } } ~~~ ----------------- ~~~ <?php namespace youwen\exwechat; /** * 错误信息容器 */ class errorContainer { /** * @var object 对象实例 */ protected static $instance; // error[] = ['code'=>'xx', 'msg'='xx']; protected static $allError=[]; protected static $lastErrorCode = 0; protected static $lastErrorMsg = ''; /** * 架构函数 * @access protected * @param array $options 参数 */ protected function __construct($options = []) { } /** * 初始化 * @access public * @param array $options 参数 * @return \loan\app\logic\errorContainer */ public static function instance($options = []) { if (is_null(self::$instance)) { self::$instance = new static($options); } return self::$instance; } /** * 获取最后一条错误码 * @author baiyouwen */ public function getLastCode() { return self::$lastErrorCode; } /** * 获取最后一条错误信息 * @author baiyouwen */ public function getLastMsg() { return self::$lastErrorMsg; } /** * 设置错误信息 * @author baiyouwen */ public function setError($code, $msg) { self::$lastErrorCode = $code; self::$lastErrorMsg = $msg; self::$allError[] = ['code'=>$code, 'msg' => $msg]; return true; } /** * 获取全部错误信息 * @author baiyouwen */ public function getAllError() { return self::$allError; } } ~~~