1. The API targets browser clients. The public parameters are login_id and login_tid, requested via GET, and their values are derived from cookies under the wildcard top-level domain, as shown in Figure 1.

2. Per the frontend developers’ request, we need to implement Cookie support for the API (not recommended). Unlike web applications, RESTful APIs are typically stateless, which means sessions or cookies should not be used.
3. In \api\behaviors\GlobalAccessBehavior.php, we support both Cookie and GET simultaneously, with GET taking precedence over Cookie. The code is as follows:
/* 获取请求参数 */
$request = Yii::$app->request;
$get = $request->get();
if (!empty($get['login_id']) && !empty($get['login_tid'])) {
$loginId = $get['login_id'];
$loginTid = $get['login_tid'];
} else {
$response = Yii::$app->response;
$acceptParams = $response->acceptParams;
if (isset($acceptParams['cookie']) && $acceptParams['cookie'] = 'enable') {
// 获取 cookie 集合
$request->enableCookieValidation = false;
$cookies = $request->cookies;
if (isset($cookies['login_chinamcloud_id'])) {
$loginId = $cookies['login_chinamcloud_id']->value;
$get['login_id'] = $loginId;
}
if (isset($cookies['login_chinamcloud_tid'])) {
$loginTid = $cookies['login_chinamcloud_tid']->value;
$get['login_tid'] = $loginTid;
}
Yii::$app->request->setQueryParams($get);
$request->enableCookieValidation = true;
/* 未登录 */
if (empty($loginId) || empty($loginTid)) {
throw new HttpException(302, Yii::t('error', '20040'), 20039);
}
}
}
4. In Postman, set the Cookie value by clicking the Cookies button, as shown in Figure 2.

5. Add the domain: api.pcs-api.localhost, as shown in Figure 3.

6. Add a Cookie. Replace Cookie_12=value in Cookie_12=value; path=/; domain=.api.pcs-api.localhost; with the corresponding key=value, as shown in Figure 4.

7. Replace Cookie_12=value with the corresponding login_chinamcloud_id=e56db1b43546a110431ac38409ed8e9e, and click the Save button, as shown in Figure 5.

8. Add login_chinamcloud_tid=3eb9d8dd0328668be6b7536019a025b3; path=/; domain=.api.pcs-api.localhost; again. Once added, close the MANAGE COOKIES popup, as shown in Figure 6.

9. Remove login_id and login_tid from the URL Params and execute the request again. The request succeeds and successfully retrieves the values from the Cookies, as shown in Figure 7.

10. Switch to the response Cookies and find that the corresponding Cookie value already exists, as shown in Figure 8.

