没有不值得去解决的问题,也没有不值得去学习的技术!

How to Implement Backward-Compatible API Workflows in the Yii 2 Framework

Based on the version information in acceptParams, write conditional code. The specific logic is: if the version is absent from the request Headers, or if the version is present and greater than or equal to 2.1, process it

1. Compatibility goal: Adjust Feiliu’s response to match Tiao’s response specification (convert non-array field types to strings) while maintaining backward compatibility.
Note: If the version is absent from the request Headers, or if the version is present and greater than or equal to 2.1, all response fields will be strings. Otherwise, the original Feiliu fields are retained.

2. Call the API using Postman, requiring the API to respond according to the version 1.2.1 convention, where the field room_status is a numeric type, as shown in Figure 1.
Note: The 1 in V1 is the major version number, the 2 in version=2.1 is the minor version number, and the 1 in version=2.1 is the revision number.

The 1 in V1 is the major version number, the 2 in version=2.1 is the minor version number, and the 1 in version=2.1 is the revision number
Figure 1

3. Now, when the version is greater than or equal to 1.2.1, all response fields must be strings. To handle the minor version number, you can use the content negotiation feature provided by the contentNegotiator behavior. The contentNegotiator behavior can set the yii\web\Response::$acceptParams property when it determines which content types are supported. For example, if a request is sent with Accept: application/json; version=2.0, after content negotiation, yii\web\Response::$acceptParams will contain the value [‘version’ => ‘2.0’]. The code is shown in Figure 2.

Now, when the version is greater than or equal to 1.2.1, all response fields must be strings. To handle the minor version number, you can use the content negotiation feature provided by the contentNegotiator behavior.
Figure 2

4. Print $acceptParams, and the result is shown in Figure 3.

Print $acceptParams
Figure 3

5. Based on the version information in acceptParams, write conditional code. The specific logic is: if the version is absent from the request Headers, or if the version is present and greater than or equal to 2.1, process it, as shown in Figure 4.

Based on the version information in acceptParams, write conditional code. The specific logic is: if the version is absent from the request Headers, or if the version is present and greater than or equal to 2.1, process it
Figure 4

6. When the version is absent from the request Headers, the API responds according to the latest version specification, and all response fields are strings, as shown in Figure 5.

When the version is absent from the request Headers, the API responds according to the latest version specification, and all response fields are strings
Figure 5

7. When the version is present in the request Headers but is less than 2.1, the API responds according to the specification for versions prior to 1.2.1, and the fields are not processed. In this case, the field room_status is a numeric type, as shown in Figure 6.

When the version is present in the request Headers but is less than 2.1, the API responds according to the specification for versions prior to 1.2.1, and the fields are not processed. In this case, the field room_status is a numeric type
Figure 6

8. When the version is present in the request Headers and is greater than or equal to 2.1, the API responds according to the latest version specification, and all response fields are strings, as shown in Figure 7.

When the version is present in the request Headers and is greater than or equal to 2.1, the API responds according to the latest version specification, and all response fields are strings
Figure 7

关于作者 · 持续学习与技术实践

我是一名拥有 15 年以上开发经验的技术从业者,自 2013 年起持续运营个人技术博客,记录工作与个人项目中遇到的真实问题,以及解决问题过程中的探索、实践和思考。

除了技术写作,我也在持续开发和维护自己的独立项目,探索新技术和新工具在实际场景中的应用。我始终相信,没有不值得去解决的问题,也没有不值得去学习的技术。

如果这篇文章对你有所帮助,欢迎浏览博客中的其他文章,也欢迎交流技术经验与想法。

关于我  ·  GitHub  ·  邮件联系