Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
bug #36063 [FrameworkBundle] start session on flashbag injection (Wil…
…liam Arslett) This PR was squashed before being merged into the 3.4 branch. Discussion ---------- [FrameworkBundle] start session on flashbag injection | Q | A | ------------- | --- | Branch? | 3.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | Fix [#33084](#33084) | License | MIT This PR addresses an issue whereby if the FlashBag is injected into the application using the default service configuration, we cannot rely that the session has been started. This behaviour is in contradiction to [the docs](https://symfony.com/doc/current/session.html#avoid-starting-sessions-for-anonymous-users): > Sessions are automatically started whenever you read, write or even check for the existence of data in the session. This is because symfony ensures the session has been started on calls to getFlashBag() which is normally how the flashbag will be accessed but this is not called if you inject the FlashBag directly into the container. I have addressed this issue by changing the way the Flashbag service is built so that it uses Session as a factory service and getFlashBag as a factory method. This means that anywhere in symfony where FlashBag is injected can now rely on the fact the session is started. I have also added a new functional test to verify this behaviour. Commits ------- e8b4d35 [FrameworkBundle] start session on flashbag injection
- Loading branch information
Showing
5 changed files
with
73 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
...undle/Tests/Functional/Bundle/TestBundle/Controller/InjectedFlashbagSessionController.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
|
||
namespace Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\TestBundle\Controller; | ||
|
||
use Symfony\Component\HttpFoundation\RedirectResponse; | ||
use Symfony\Component\HttpFoundation\Request; | ||
use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface; | ||
use Symfony\Component\Routing\RouterInterface; | ||
|
||
class InjectedFlashbagSessionController | ||
{ | ||
/** | ||
* @var FlashBagInterface | ||
*/ | ||
private $flashBag; | ||
|
||
/** | ||
* @var RouterInterface | ||
*/ | ||
private $router; | ||
|
||
public function __construct( | ||
FlashBagInterface $flashBag, | ||
RouterInterface $router | ||
) { | ||
$this->flashBag = $flashBag; | ||
$this->router = $router; | ||
} | ||
|
||
public function setFlashAction(Request $request, $message) | ||
{ | ||
$this->flashBag->add('notice', $message); | ||
|
||
return new RedirectResponse($this->router->generate('session_showflash')); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters