[Pandaman] #[RequestCompletion] 注解的应用场景

wasoncheung

[RequestCompletion] 的执行时机是 每个请求的最后阶段。此时前端内容早已响应完毕

本例中 是用来记录后台的操作日志

/**
 * @Date: 2023/3/18
 * @Author: WasonCheung
 * 操作日志记录
 */
class AdminOperationLogger
{
    /**
     * @var AdminLogEntity[]
     */
    public static array $logs = [];

    public static function addRecord(AdminLogEntity $adminLogEntity): void
    {
        self::$logs[] = $adminLogEntity;
    }

    #[RequestCompletion]
    public static function save(): void
    {
        foreach (self::$logs as $adminLogEntity) {
            $logs[] = $adminLogEntity->toArray(true);
        }
        if (isset($logs)) {
            AdminLogModel::saveAll($logs);
        }
    }
}
435 0 0
0个评论

wasoncheung

1070
积分
0
获赞数
0
粉丝数
2022-11-15 加入
🔝