Home > Backend Development > PHP Tutorial > Restful api error prompt return value implementation ideas in PHP restful api example restful interface jersey restful

Restful api error prompt return value implementation ideas in PHP restful api example restful interface jersey restful

WBOY
Release: 2016-07-29 08:53:33
Original
821 people have browsed it

RESTful architecture is a popular Internet software architecture. It has a clear structure, conforms to standards, is easy to understand, and is easy to expand.

REST is the abbreviation of Representational State Transfer, which translates as "presentation layer state transformation". The presentation layer is actually a resource, so it can be understood as "resource status transformation".

Any entity on the web application can be regarded as a resource, pointed to by a URI (Uniform Resource Locator).

Preface

Whether it is Weibo or Taobao, they have their own error return value format specifications and error code descriptions, which are not only convenient to use on mobile phones, but also give people a clear and high-level feeling. When we encounter problems, we should first go to the parent company. The norms of large companies are the mother parent for us to refer to. To this end, I imitated Taobao's error return value format, customized my own error code based on the standards set by Weibo error codes, and then tested it on the Restful api. Below I will share with you the implementation ideas and test results.

Implementation idea

I use the abstract factory pattern to implement such an error return value. I chose this pattern because it provides an interface for creating a series of related or interdependent objects, which is very close to my needs.

Code Analysis

1. According to this path commonhint, I created a new error folder to store my error prompt program files. This folder mainly contains these files:

2. Hint.php entry file. Define an abstract class and write only one method in it.

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

3. Template.php implements the Hint interface. The format of the error return value is defined here.

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 Create another createMsg abstract class. Abstract object creation into an interface.

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

5. Use FactoryMsg class to implement the createMsg interface. Returns the instantiated Template.

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

6. ErrorMsg.php passes parameters to the Error method in Template.

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. Calling method.

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

8. Test results.

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

Done. I use the form of language packs for the entire implementation process, which will facilitate multi-language switching in the future.

FAQ

1. Using this kind of string fuzzy search is very general and cannot meet the requirements of returning specific codes corresponding to specific error types. If you have better suggestions, everyone is welcome.

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

2. During the implementation process, the problem of multi-language switching in the future was not considered, and the prompts were directly transmitted in the traditional way. For example: ErrorMsg::Info("Operation failed"); In this way, multi-language switching cannot be achieved. It is recommended that you use language packs to pass parameters.

The above introduces the idea of ​​implementing the error prompt return value of Restful api in PHP, including restful content. I hope it will be helpful to friends who are interested in PHP tutorials.

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 Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template