Skip to content

Commit

Permalink
fix(flashbag): fixed addFlash in ControllerTrait for Symfony 6
Browse files Browse the repository at this point in the history
  • Loading branch information
UlrichHP committed Feb 3, 2023
1 parent 7d0ea4b commit 8b8022d
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions src/Bundle/Controller/ControllerTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,13 @@
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\HttpFoundation\BinaryFileResponse;
use Symfony\Component\HttpFoundation\Exception\SessionNotFoundException;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\ResponseHeaderBag;
use Symfony\Component\HttpFoundation\Session\FlashBagAwareSessionInterface;
use Symfony\Component\HttpFoundation\StreamedResponse;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\HttpKernel\HttpKernelInterface;
Expand Down Expand Up @@ -156,13 +158,19 @@ protected function file($file, string $fileName = null, string $disposition = Re
*
* @final
*/
protected function addFlash(string $type, $message)
protected function addFlash(string $type, mixed $message): void
{
if (!$this->container->has('session')) {
throw new \LogicException('You can not use the addFlash method if sessions are disabled. Enable them in "config/packages/framework.yaml".');
try {
$session = $this->container->get('request_stack')->getSession();
} catch (SessionNotFoundException $e) {
throw new \LogicException('You cannot use the addFlash method if sessions are disabled. Enable them in "config/packages/framework.yaml".', 0, $e);
}

$this->container->get('session')->getFlashBag()->add($type, $message);
if (!$session instanceof FlashBagAwareSessionInterface) {
trigger_deprecation('symfony/framework-bundle', '6.2', 'Calling "addFlash()" method when the session does not implement %s is deprecated.', FlashBagAwareSessionInterface::class);
}

$session->getFlashBag()->add($type, $message);
}

/**
Expand Down

0 comments on commit 8b8022d

Please sign in to comment.