php接口返回一个数组怎末写_php 给接口返回数据

/**

* 输出错误信息

* @param int $code

* @param $msg

* @throws BaseException

*/

protected function throwError($msg, $code = 0)

{

throw new BaseException(['code' => $code, 'msg' => $msg]);

}

/**

* 返回封装后的 API 数据到客户端

* @param int $code

* @param string $msg

* @param array $data

* @return array

*/

protected function renderJson($code = self::JSON_SUCCESS_STATUS, $msg = '', $data = [])

{

return compact('code', 'msg', 'url', 'data');

}

/**

* 返回操作成功json

* @param string $msg

* @param array $data

* @return array

*/

protected function renderSuccess($data = [], $msg = 'success')

{

return $this->renderJson(self::JSON_SUCCESS_STATUS, $msg, $data);

}

/**

* 返回操作失败json

* @param string $msg

* @param array $data

* @return array

*/

protected function renderError($msg = 'error', $data = [])

{

return $this->renderJson(self::JSON_ERROR_STATUS, $msg, $data);

}

class BaseException extends Exception {

public $code = 400;

public $msg = 'invalid parameters';

public $errorCode = 500;

/**

* 构造函数,接收一个关联数组

* @param array $params 关联数组只应包含code、msg和errorCode,且不应该是空值

*/

public function __construct($params = []) {

if (!is_array($params)) {

return;

}

if (array_key_exists('code', $params)) {

$this->code = $params['code'];

}

if (array_key_exists('msg', $params)) {

$this->msg = $params['msg'];

}

if (array_key_exists('errorCode', $params)) {

$this->errorCode = $params['errorCode'];

}

}

}

我想知道什么实用用异常类 我感觉都可以用 rendersuccess 返回数据吧 因为数据结构都一样的

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值