Home >PHP Framework >ThinkPHP >How to encapsulate and optimize api data format in thinkphp6
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.
参数解析: 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.
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.
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
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!