What should I pay attention to when writing RestApi?

  • PS1 = "\ n [\ e [32; 1m] ([\ e [37; 1m] \ u [\ e [32; 1m]) - [[e [37; 1m] jobs: \ j [\ e ([32; 1m]) - (\ \ e [37; 1m] \ w [\ e [32; 1m]) \ n ([[\ e [37; 1m]!! [\ E [32; 1m]) -> "
  • The business is responsible for the model layer, and the control layer is only responsible for calling 

  • Use XMind to draw mind maps, ask questions and answer yourself, and deepen your familiarity with the software and use skills in practice

  • It is recommended to use soft foreign key constraints and soft deletes when designing database tables; at the same time, it is difficult to consider the completeness of database tables at the beginning, and some field changes may also be made during the development process.

  • When accessing the url link in Postman , you can also pull up the breakpoint debugging of phpstrom by adding the suffix of ?DEBUG SESSION STAER directly

  • How to consider starting to write an interface?

  • From a certain operation in the actual application, such as login, jump, etc., can correspond to a certain business logic, think about which parameters are required, and write the corresponding method. At the beginning, you can consider writing it in the Controller for testing, and then transfer to Go to Model

  • What needs to be considered?

  • Different versions v1, v2; the method accepts the validation of parameters (the idea of ​​​​the validator may extract some public methods into the base class BaseValidate); exceptions that may occur during the entire api call process

  • The better the code encapsulation, the more abstract, the better the code reusability

  • @ RESTFul API

  • Resource-based, CRUD is based on changes to the state of the resource; use HTTP verbs to manipulate resources (/getmoive/:mid GET:/moive/:mid recommends the latter)

  • @ HTTP verbs POST: CREATE PUT: UPDATE GET: QUERY DELETE: DELETE

  • Status code: 404(Not Found), 400(Parameters Error), 200, 201, 202, 401(Unauthorized), 403, 500

  • Error code: custom error ID number

  • Unified error description: error code, error message, URL where the current error message occurred

  • Use Tokens to Authorize and Verify Identity

  • version control

  • Tests are separated from production: api.xxx.com/dev.api.xxx.com

  • The semantics of the URL should be clear, it is best to "see the text and understand the meaning"

  • It is better to have a relatively standard document

  • The best way to learn: imitate Douban open api; github developer api

  • API is authenticated and authorized by Token and token. Three points must be considered: Is the Token valid (there is a corresponding row in the database)? The validity period of the Token; the permissions corresponding to the Token

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325635300&siteId=291194637