REST is the abbreviation of Representational State Transfer, translated as "presentation layer state transformation". The presentation layer is actually a resource, so it can be understood as "resource status transformation". Next, through this article, I will introduce to you the implementation ideas of Restful api error prompt return value. Interested friends can learn together
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, translated as "presentation layer state transformation". The presentation layer is actually a resource, so it can be understood as "resource status transformation".
Any entity on a network 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. This not only The mobile version is easy to use and feels clear and high-end. 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 ideas
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 common\hint, I created a new error folder to store my error prompt program files. There are mainly these files in this folder:
2, Hint.php entry file. Define an abstract class and write only one method in it.
interface Hint { function Error($_errors,$code); }
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)); } } }
4. createMsg.php Create another createMsg abstract class. Abstract the creation of objects into an interface.
interface createMsg { function Msg(); }
5. Use the FactoryMsg class to implement the createMsg interface. Returns the instantiated Template.
class FactoryMsg implements createMsg{ function Msg() { return new Template; } }
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'); } }
7. Calling method.
use common\hint\error\ErrorMsg; ErrorMsg::Info(Yii::t('yii','failure'));
8. Test results.
{ "error": { "name": "Not Found", "message": "操作失败", "error_code": "20003" } }
Done. I use the form of language packs for the entire implementation process, which will facilitate later multi-language switching.
Frequently Asked Questions
#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, you are welcome to make them.
$result = strstr($_errors,Yii::t('yii','Not exist'));
2. During the implementation process, the issue of multi-language switching in the future was not taken into consideration, and prompts were directly transmitted in the traditional way. For example: ErrorMsg::Info("Operation failed"); Multi-language switching cannot be achieved in this way. It is recommended that you use language packs to pass parameters.
Summary: The above is the entire content of this article, I hope it will be helpful to everyone's study.
Related recommendations:
phpUse curl to obtain remote images implementation method
phpImplementing the method and simple example of obtaining the check box value
phpDetailed graphic and text explanation of the method of using text to count visits
The above is the detailed content of How to implement error prompt return value in Restful api in PHP. For more information, please follow other related articles on the PHP Chinese website!