php - Save flash message on database after "addFlash" method is called on Symfony Controller
Get the solution ↓↓↓I have a Symfony service to add an alert (bootstrap alert in DOM) to database to allow administrators to audit the usage of the application and also this service saves the alert in session storage to display the alerts until the user closes each alert.
This is the code for the service method:
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;
}
My question is if Symfony has an event listener to automatically call this service method each time I write the following lines on controllers:
$this->addFlash('success', $message);
$this->addFlash('error', $message);
$this->addFlash('warning', $message);
Now, after I add a message to the Flash Bag message, my controller executes the following instruction:
$this->alertsService->addAlert($datetimeUTCNow, $eventType, $message, $user, $alertId);
Do you know any event listener to avoid writing this line each time the controller calls to addFlash method? I am using Symfony 5 and PHP 7.3
Thank you.
Answer
Solution:
Here is my solution to override the addFlash method:
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);
}
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: xml parsing error: no root element found
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.