Skip to content

Commit

Permalink
fix #538 fix(flashbag): fixed addFlash in ControllerTrait for Symfony…
Browse files Browse the repository at this point in the history
… 6 (UlrichHP)

This PR was merged into the 1.10 branch.

Discussion
----------

| Q               | A
| --------------- | -----
| Bug fix?        | yes
| New feature?    | no
| BC breaks?      | no
| Deprecations?   | no <!-- don't forget to update the UPGRADE-*.md file -->
| Related tickets | fixes #535 
| License         | MIT


Commits
-------

8b8022d fix(flashbag): fixed addFlash in ControllerTrait for Symfony 6
ef28368 fix(flashbag): removed potential BC from addFlash function
  • Loading branch information
lchrusciel authored Sep 9, 2023
2 parents 3c5afed + ef28368 commit c8f12d3
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 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 @@ -158,11 +160,17 @@ protected function file($file, string $fileName = null, string $disposition = Re
*/
protected function addFlash(string $type, $message)
{
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 c8f12d3

Please sign in to comment.