Table of Contents
Solution
Create file
Using variables
Home PHP Framework ThinkPHP How to encapsulate and optimize api data format in thinkphp6

How to encapsulate and optimize api data format in thinkphp6

Apr 20, 2021 am 09:48 AM
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.

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进行描述。
Copy after login

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 of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)