RESTful Architecture is a popular Internet software architecture, it has a clear structure, conforms to the standard, is easy to understand, expands conveniently.
Rest is the abbreviation for the Representational state transfer, translated as "Presentation layer status transformation". The presentation layer is actually a resource, so it can be understood as "resource state transformation".
Any entity on a network application can be considered a resource, pointing to it through a URI (Uniform Resource Locator).
Preface
Whether it is Weibo or Taobao, they have their own error return value format specification, as well as error code description, so that not only the phone to use the convenience of the feeling is clear, tall. Encounter problems to find the female parent first, the norms of large companies is our reference to the female parent. To this end, I modeled Taobao error return value format, according to the micro-blog error code established by the standards of their own error code, and then on the RESTful API to test. Below I will realize the idea and the test result share to everybody.
Realize the idea
I use the abstract factory pattern to implement such an error return value. This pattern is chosen because it allows you to provide an interface to create a series of related or interdependent objects that are close to my needs.
Code Analysis
1, according to this path common\hint, I created a new error folder to store my errors prompted the program file. These files are mainly available in this folder:
2, hint.php import documents. Define an abstract class that has only one method written inside.
Interface Hint {
function Error ($_errors, $code);
}
3, template.php implementation hint this 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 again create a createmsg abstract class. Abstracts the creation of an object into an interface.
Interface Createmsg {
function Msg ();
}
5, with factorymsg class to implement Createmsg interface. Returns the instantiated template.
Class Factorymsg implements createmsg{
function Msg () {return
new Template;
}
}
6, errormsg.php to template inside the error method to pass the parameter.
Class ErrorMsg {//The static method in the abstract factory public static function Info ($_errors) {$Factory = new factorymsg; $result = Strstr ($_err Ors,yii::t (' Yii ', ' not exist ')); Data does not exist 20001 $result 1 = strstr ($_errors,yii::t (' Yii ', ' Null ')); Parameter cannot be empty 20002 $result 2 = strstr ($_errors,yii::t (' Yii ', ' Fail ')); Add, update, delete failed 20003 $result 3 = strstr ($_errors,yii::t (' Yii ', ' not Right ')); XX incorrect 20004 $result 4 = strstr ($_errors,yii::t (' Yii ', ' ROBC ')); XX no permission 20005//data does not exist 20001 if (!empty ($result)) {$M = $Factory->msg (); $M->error ($_errors, ' 20001 ');d ie;}//parameter cannot be empty 20002 if (!empty ($result 1)) {$M = $Factory->msg (); $M->error ($_errors, ' 20002 ');d ie;}//New, UPDATE, delete failed 20003 if (!empty ($result 2))
{$M = $Factory->msg (); $M->error ($_errors, ' 20003 ');d ie;} XX incorrect 20004 if (!empty ($result 3)) {$M = $Factory->msg (); $M->error ($_errors, ' 20004 ');d ie;//xx no permissions 20005 if (!emp
Ty ($result 4)) {$M = $Factory->msg (); $M->error ($_errors, ' 20005 ');d ie;}//default type 21000 $M = $Factory->msg (); $M->error ($_errors,' 21000 '); }
}
7, call the way.
Use common\hint\error\errormsg;
Errormsg::info (yii::t (' Yii ', ' failure '));
8, test results.
{'
error ': {
' name ': ' Not Found ',
' message ': ' Operation failed ',
' error_code ': ' 20003 '
}
}
Complete. The whole implementation process I use the form of language packs, which is conducive to the late multiple language switching.
Problems
1, the use of this string fuzzy search is very broad, can not achieve the specific error type return the corresponding specific code requirements. If you have better suggestions, you are welcome to propose.
$result = Strstr ($_errors,yii::t (' Yii ', ' not exist '));
2, the implementation of the process does not take into account the future of multiple language switching problems, and then directly in the traditional way to pass the prompt language. For example: Errormsg::info ("Operation failed"), this is not possible to achieve multiple language switching. We recommend that you use the language pack to pass the reference.