Home > php教程 > php手册 > body text

PHP中Restful api 错误提示返回值实现思路,restfulapi

WBOY
Release: 2016-06-13 08:42:01
Original
1316 people have browsed it

PHP中Restful api 错误提示返回值实现思路,restfulapi

RESTful架构是一种流行的互联网软件架构,它结构清晰,符合标准,易于理解,扩展方便。

REST是Representational State Transfer的缩写,翻译为“表现层状态转化”。表现层其实就是资源,因此可以理解为“资源状态转化”。

网络应用上的任何实体都可以看作是一种资源,通过一个URI(统一资源定位符)指向它。

序言

不管是微博还是淘宝,他们都有自己的错误返回值格式规范,以及错误代码说明,这样不但手机端用起来方便,给人的感觉也清晰明了,高大上。遇到问题先找母本,大公司的规范就是我们参照的母本。为此,我仿照了淘宝的错误返回值格式,根据微博错误代码制定的标准自定了自己的错误代码,然后在Restful api 上进行测试。下面我将实现思路以及测试结果分享给大家。

实现思路

我利用抽象工厂模式去实现这样的一个错误返回值。选择这种模式是因为考虑到了这种模式可以提供一个创建一系列相关或相互依赖对象的接口,与我的需求很接近。

代码分析

1、按这个路径common\hint,我新建了个error文件夹存放我的错误提示程序文件。这文件夹中主要有这几个文件:

2、Hint.php入口文件。定义一个抽象类,里边只写一个方法。

interface Hint {
function Error($_errors,$code);
}
Copy after login

3、Template.php 实现Hint这个接口。错误返回值的格式就在这里定义。

class Template implements Hint{
function Error($_errors,$code) { 
if (empty($_errors)) {
print_r(json_encode([]));
} else { 
$errors['error']['name'] = 'Not Found';
$errors['error']['message'] = $_errors;
$errors['error']['error_code'] = $code; 
print_r(json_encode($errors));
}
}
}
Copy after login

4、createMsg.php 再创建一个createMsg抽象类。将对象的创建抽象成一个接口。

interface createMsg { 
function Msg(); 
}
Copy after login

5、用FactoryMsg 类去实现createMsg接口。返回实例化的Template。

class FactoryMsg implements createMsg{
function Msg() {
return new Template;
}
}
Copy after login

6、ErrorMsg.php 给Template里边的Error方法传参。

class ErrorMsg {
// 抽象工厂里的静态方法
public static function Info($_errors) { 
$Factory = new FactoryMsg;
$result = strstr($_errors,Yii::t('yii','Not exist')); //数据不存在 20001
$result1 = strstr($_errors,Yii::t('yii','Null')); //参数不能为空 20002
$result2 = strstr($_errors,Yii::t('yii','Fail')); //新增、更新、删除失败 20003
$result3 = strstr($_errors,Yii::t('yii','Not right')); //XX不正确 20004
$result4 = strstr($_errors,Yii::t('yii','Robc')); //XX无权限 20005
//数据不存在 20001
if(!empty($result)){ 
$M = $Factory->Msg();
$M->Error($_errors,'20001');die;
}
//参数不能为空 20002
if(!empty($result1)){ 
$M = $Factory->Msg();
$M->Error($_errors,'20002');die;
}
//新增、更新、删除失败 20003
if(!empty($result2)){ 
$M = $Factory->Msg();
$M->Error($_errors,'20003');die;
}
//XX不正确 20004
if(!empty($result3)){ 
$M = $Factory->Msg();
$M->Error($_errors,'20004');die;
}
//XX无权限 20005
if(!empty($result4)){ 
$M = $Factory->Msg();
$M->Error($_errors,'20005');die;
}
//默认类型 21000
$M = $Factory->Msg();
$M->Error($_errors,'21000');
}
}
Copy after login

7、调用方式。

use common\hint\error\ErrorMsg;
ErrorMsg::Info(Yii::t('yii','failure'));
Copy after login

8、测试结果。

{
"error": {
"name": "Not Found",
"message": "操作失败",
"error_code": "20003"
}
}
Copy after login

完成。整个实现过程我采用语言包的形式,这样有利于后期多语言的切换。

常见问题

1、采用这种字符串模糊搜索很泛,无法达到具体错误类型返回对应具体代码的要求。如有更好的建议,欢迎大家提议。

$result = strstr($_errors,Yii::t('yii','Not exist'));
Copy after login

2、实现过程中没有考虑到今后多语言切换的问题,然后直接用传统的方式传提示语。比如:ErrorMsg::Info("操作失败");这样是无法实现多语言切换的。建议大家用语言包的方式传参。

您可能感兴趣的文章:

  • PHP实现自动识别Restful API的返回内容类型
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!