【问题标题】:Save flash message on database after "addFlash" method is called on Symfony Controller在 Symfony 控制器上调用“addFlash”方法后在数据库中保存 flash 消息
【发布时间】:2020-07-25 17:30:13
【问题描述】:

我有一个 Symfony 服务向数据库添加警报(DOM 中的引导警报),以允许管理员审核应用程序的使用情况,并且此服务将警报保存在会话存储中以显示警报,直到用户关闭每个警报.

这是服务方法的代码:

public function addAlert(DateTimeInterface $eventDatetime, string $eventType, string $data, UserInterface $user, string $alertId): bool
{
    $success = false;
    try {
        //Build alert entity
        $alert = new UserAlert();
        $alert->setDatetimeUTC($eventDatetime);
        $alert->setEventType($eventType);
        $alert->setMessage($data);
        $alert->setUser($user);

        //Save in database
        $this->entityManager->persist($alert);
        $this->entityManager->flush();

        //Save in session
        $success = $this->saveAlertInSession($alertId, $eventType, $data);
    } catch (Throwable $throwable) {
        $this->logger->critical("Add User Alert Service: Ko. Details: {$throwable->getMessage()}.");
    }

    return $success;
}

private function saveAlertInSession(string $alertId, string $eventType, string $data): bool
{
    $alerts = [];
    try {
        //Check if any alert is stored to retrieve
        if ($this->session->has('alerts')) {
            $alerts = $this->session->get('alerts');
        }

        //Add new alert
        $alerts[$alertId] = [
            'type' => $eventType,
            'data' => $data,
        ];

        //Save to session
        $this->session->set('alerts', $alerts);

        $success = true;
    } catch (Throwable $throwable) {
        $success = false;
        $message = "Save Alert Session: Ko. Details: {$throwable->getMessage()}.";
        $this->logger->critical($message);
    }

    return $success;
}

我的问题是 Symfony 是否有一个事件侦听器,可以在我每次在控制器上编写以下行时自动调用此服务方法:

$this->addFlash('success', $message);
$this->addFlash('error', $message);
$this->addFlash('warning', $message);

现在,在我向 Flash Bag 消息添加消息后,我的控制器执行以下指令:

$this->alertsService->addAlert($datetimeUTCNow, $eventType, $message, $user, $alertId);

你知道任何事件监听器来避免每次控制器调用 addFlash 方法时都写这行吗?我正在使用 Symfony 5 和 PHP 7.3

谢谢。

【问题讨论】:

  • 你使用的是什么版本的 Symfony?
  • @HamidAli 我正在使用 Symfony 5.0.7
  • 你可以用你自己的实现覆盖AbstractController中的addFlash方法。

标签: php symfony session


【解决方案1】:

这是我重写 addFlash 方法的解决方案:

protected function addFlash(string $type, string $message): void
{
    // Generate the required arguments on userAlerts service
    try {
        $datetimeUTC = new DateTime('now', new DateTimeZone('UTC'));
        $alertId = Uuid::uuid4();

        $this->userAlertsService->addAlert($datetimeUTC, $type, $message, $this->getUser(), $alertId);
    } catch (Throwable $throwable) {
        $this->logger->critical("Add Flash: Ko. Details: {$throwable->getMessage()}");
    }

    parent::addFlash($type, $message);
}

【讨论】:

    猜你喜欢
    • 2012-11-01
    • 1970-01-01
    • 2017-05-31
    • 2015-09-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-26
    • 1970-01-01
    相关资源
    最近更新 更多