A backwards compatible process for interfaces to implement in Yii 2 framework
1. Compatibility goal: adjust the response of the flying flow to the response specification of the body (convert the field type of the non-array to a string), and be backwards compatible
Note: If the version of the requested headers does not exist or the version exists and the version is greater than or equal to 2.1, the response fields are all strings. Otherwise, it is the original field of the flying flow.
2. Based on the postman call interface, the interface is required to respond according to the convention of version number 1.2.1, where the field room_status is the numeric type, as shown in Figure 1
Note: 1 in V1 is the main version number, 2 in version=2.1 is the minor version number, and 1 in version=2.1 is the revision number
3. Now it is necessary to use the content negotiation function to provide the behavior provided by ContentNegotiator when the version is greater than or equal to 1.2.1, the response fields are all strings. The contentNegotiator behavior can be set to the yii\web\response::$acceptParams property when it determines which content types are supported. For example, if a request is sent via accept: application/json; version=2.0, after the content is negotiated, yii\web\response::$acceptParams will contain the value[‘version’ => ‘2.0’]. The code is shown in Figure 2
4. Print $acceptParams, the result is shown in Figure 3
5. Based on the version information of AcceptParams, write condition code, the specific meaning is: the version in the requested headers does not exist or the version exists and the version exists and is greater than or equal to 2.1, then process, as shown in Figure 4
6. When the version of the requested headers does not exist, the interface responds according to the latest version specification, and the response fields are all strings, as shown in Figure 5
7. When the version exists in the requested headers, but is less than 2.1, the interface responds according to the specification of less than 1.2.1, and the fields are not processed, and the field room_status is the numeric type, as shown in Figure 6
8. When the version of the requested headers exists and is greater than or equal to 2.1, the interface responds according to the latest version specification, and the response fields are all strings, as shown in Figure 7






