Implementation of setting and getting global variables in Yii 2.0 (based on yii::$app->params)
1. At this stage, there is a requirement, and it is necessary to send the corresponding operation logs generated to the log system of the middle platform in an interface call. The log system interface of Zhongtai supports multiple log records at the same time. Therefore, it is decided to plan to first save the generated N operation logs in the interface call, and first store them in the global variable, and then at the end of the interface call, get the log records in the global variable, one HTTP request, and send it to the log system of the middle stage. as shown in Figure 1
2. Reference URL:https://www.yiiframework.com/doc/guide/2.0/zh-cn/structure-applications#params. yii::$app->params. as shown in Figure 2
3. Edit the log template message and configure the corresponding template. Edit the corresponding language package file: common/messages/en-cn/application.php
'新建选题【{plan_title}】',
302201 => '启用选题【{plan_title}】',
302202 => '禁用选题【{plan_title}】',
302203 => '提交选题【{plan_title}】进行审核',
302204 => '编辑选题【{plan_title}】',
302205 => '删除选题【{plan_title}】',
302206 => '通过选题【{plan_title}】',
302207 => '拒绝选题【{plan_title}】',
302208 => '指派【{plan_task_title}】{plan_task_type}给【{user_nick}】',
302209 => '启用【{plan_task_title}】{plan_task_type}',
302210 => '禁用【{plan_task_title}】{plan_task_type}',
302211 => '删除【{plan_task_title}】{plan_task_type}',
302212 => '新建【{plan_task_title}】{plan_task_type}指派给【{user_nick}】',
302213 => '编辑【{plan_task_title}】{plan_task_type}',
302214 => '转派【{plan_task_title}】{plan_task_type}给【{user_nick}】',
302215 => '认领【{plan_task_title}】{plan_task_type}',
];
4. Classify the log messages accordingly, edit: common/config/params.php
[ //框架服务接口日志操作类型
[
'operateKey' => 'plan_management', //操作类型标识
'operateTitle' => '选题管理', //操作类型名称
'codes' => [302200, 302201, 302202, 302203, 302204, 302205], //日志详情的返回码
],
[
'operateKey' => 'plan_review', //操作类型标识
'operateTitle' => '选题审核', //操作类型名称
'codes' => [302206, 302207], //日志详情的返回码
],
[
'operateKey' => 'plan_task_management', //操作类型标识
'operateTitle' => '任务管理', //操作类型名称
'codes' => [302208, 302209, 302210, 302211, 302212, 302213, 302214, 302215], //日志详情的返回码
]
],
];
5. Write a method of automatically generating logs global variables
/**
* 自动创建上报日志信息,放入(Yii::$app->params['cmcApiLogs']),为后续 HTTP 请求准备数据
* @param int $code 返回码
* @param array $data 数据,需要被替换的变量
* 格式如下:
* [
* 'plan_title' => '选题名称',
* ]
*
* @param object $identity 当前用户的身份实例
*/
public static function automaticCreate($code, $data, $identity)
{
foreach (Yii::$app->params['cmcApiLogOperates'] as $operateValue) {
foreach ($operateValue['codes'] as $codeValue) {
if ($codeValue == $code) {
$operateKey = $operateValue['operateKey'];
$operateTitle = $operateValue['operateTitle'];
}
}
}
if (isset($operateKey) && isset($operateTitle)) {
Yii::$app->params['cmcApiLogs'][] = [
'user_token' => $identity->user_token,
'service_key' => Yii::$app->params['cmcApi']['serviceKey'],
'log_type' => 'operate',
'operate_key' => $operateKey,
'operate_title' => $operateTitle,
'log_detail' => Yii::t('application', Yii::t('application', Yii::t('application', $code), $data)),
'log_time' => date('Y-m-d H:i:s'),
'log_ip' => '0.0.0.0',
];
}
}
6. Check whether there exists in the trigger event_after_request event, and print yii::$app->params if it exists[‘cmcApiLogs’]. There are 2 log records, you can get the log records in the global variable in the trigger event_after_request event, one HTTP request, and send it to the log system of the middle station. as shown in Figure 3
Array
(
[0] => Array
(
[user_token] => 3cf1c3d1db4951ad3179e1452580c0c3
[service_key] => pcs
[log_type] => operate
[operate_key] => plan_management
[operate_title] => 选题管理
[log_detail] => 新建选题【选题 20211015 0】
[log_time] => 2021-10-15 14:02:00
[log_ip] => 0.0.0.0
)
[1] => Array
(
[user_token] => 3cf1c3d1db4951ad3179e1452580c0c3
[service_key] => pcs
[log_type] => operate
[operate_key] => plan_management
[operate_title] => 选题管理
[log_detail] => 提交选题【选题 20211015 0】进行审核
[log_time] => 2021-10-15 14:02:00
[log_ip] => 0.0.0.0
)
)


![在 触发 EVENT_AFTER_REQUEST 事件中判断是否存在,如果存在则打印 Yii::$app->params['cmcApiLogs']。存在 2 条日志记录,可以在 触发 EVENT_AFTER_REQUEST 事件中 获取全局变量中的日志记录,一次 HTTP 请求,一并发送给中台的日志系统。](https://www.shuijingwanwq.com/wp-content/uploads/2021/10/3-4.png)