Home >PHP Framework >ThinkPHP >How to encapsulate and optimize api data format in thinkphp6

How to encapsulate and optimize api data format in thinkphp6

藏色散人
藏色散人forward
2021-04-20 09:48:572601browse

The following tutorial column of thinkphp will introduce to you how to encapsulate and optimize the api data format in thinkphp6. I hope it will be helpful to friends in need!

The Common.php file in the app root directory stores public methods. The methods defined here, Can be used in files in any program.

How to encapsulate and optimize api data format in thinkphp6
How to encapsulate and optimize api data format in thinkphp6

参数解析:
status  状态码,一般是0或者是1,0代表错误,1代表正确,有数据返回。

message  错误信息

httpstatus  描述http请求,200表示正常,404表示页面不存在,这些都可以在文件中约定

data   返回数据,如果数据为空,可以使用null进行描述。

However, at this time, there are common uses in this method, such as 0 or 1 in status, http status code 200, we can These constants are extracted into a unified file, so that we can make subsequent modifications conveniently.

Solution

Create file

The directory in config is specially used to store our configuration files. Therefore, we can create a file in config The file is specifically used to store these constants.

How to encapsulate and optimize api data format in thinkphp6

Using variables

We can use the config() method to read the class files in the configuration file. status represents the class file. Get the attribute value corresponding to the attribute in this class by clicking.
As shown below
How to encapsulate and optimize api data format in thinkphp6
In fact, it is not difficult to understand this way of writing. We can regard the config directory as a method, and each file as an object. If there is an object, there must be Properties and methods, therefore, we can get the property values ​​corresponding to the properties of the object through points.

Related recommendations: The latest 10 thinkphp video tutorials

The above is the detailed content of How to encapsulate and optimize api data format in thinkphp6. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete