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

Implement Global Variables in Yii 2.0 With Yii::$app->params

Check whether it exists when the EVENT_AFTER_REQUEST event is triggered; if it exists, print Yii::$app->params['cmcApiLogs']. There are 2 log records, so you can retrieve the log records from the global variable when the EVENT_AFTER_REQUEST event is triggered, and send them to the middle platform's log system together in a single HTTP request.

1. Currently, there is a requirement to send the generated operation logs to the middle platform’s log system during a single API call. The middle platform’s log system API supports receiving multiple log records simultaneously. Therefore, I decided to first store the N operation logs generated during an API call in a global variable, and then, when the API call ends, retrieve the log records from the global variable and send them to the middle platform’s log system together in a single HTTP request. As shown in Figure 1

Currently, there is a requirement to send the generated operation logs to the middle platform's log system during a single API call. The middle platform's log system API supports receiving multiple log records simultaneously. Therefore, I decided to first store the N operation logs generated during an API call in a global variable, and then, when the API call ends, retrieve the log records from the global variable and send them to the middle platform's log system together in a single HTTP request.
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

Reference URL: https://www.yiiframework.com/doc/guide/2.0/zh-cn/structure-applications#params . Yii::$app->params.
Figure 2

3. Edit the log template messages and configure the corresponding templates. Edit the corresponding language pack file: common/messages/zh-CN/application.php

PHP
<?php
return [
    302200 => '新建选题【{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. Categorize the log messages accordingly, edit: common/config/params.php

PHP
<?php
return [
    'cmcApiLogOperates' => [ //框架服务接口日志操作类型
        [
            '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 to automatically generate the global variable for logs

PHP

	/**
	 * 自动创建上报日志信息,放入(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 it exists when the EVENT_AFTER_REQUEST event is triggered; if it exists, print Yii::$app->params[‘cmcApiLogs’]. There are 2 log records, so you can retrieve the log records from the global variable when the EVENT_AFTER_REQUEST event is triggered, and send them to the middle platform’s log system together in a single HTTP request. As shown in Figure 3

Check whether it exists when the EVENT_AFTER_REQUEST event is triggered; if it exists, print Yii::$app->params['cmcApiLogs']. There are 2 log records, so you can retrieve the log records from the global variable when the EVENT_AFTER_REQUEST event is triggered, and send them to the middle platform's log system together in a single HTTP request.
Figure 3
Plaintext
Array
(
    [0] => Array
        (
            [user_token] => 3cf1c3d1db4951ad3179e1452580c0c3
            [service_key] => pcs
            [log_type] => operate
            [operate_key] => plan_management
            [operate_title] => Topic Management
            [log_detail] => Create new topic [Topic 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] => Topic Management
            [log_detail] => Submit topic [Topic 20211015 0] for review
            [log_time] => 2021-10-15 14:02:00
            [log_ip] => 0.0.0.0
        )

)


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

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

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

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

关于我  ·  GitHub  ·  邮件联系