Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[HttpFoundation] FlashbagAwareSessionInterface #41765

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
shyim opened this issue Jun 21, 2021 · 5 comments · Fixed by #46491
Closed

[HttpFoundation] FlashbagAwareSessionInterface #41765

shyim opened this issue Jun 21, 2021 · 5 comments · Fixed by #46491

Comments

@shyim
Copy link
Contributor

shyim commented Jun 21, 2021

Description
The current SessionInterface does not contain the method getFlashBag. As the DI way has been deprecated it's difficult to get correctly the FlashBag.

The workaround currently is

// Returns SessionInterface without getFlashBag
$session = $request->getSession();

if (method_exists($session, 'getFlashBag') {
$session->getFlashBag()->fooo();
}

or asserting that is a actual Session object which makes decoration shitty

Example

Introduce an additional interface to access the method getFlashBag on the interface level and not implementation.

@COil
Copy link
Contributor

COil commented Jun 29, 2021

Agree, I do this, yes, this is a little frustrating :

    // use Symfony\Component\HttpFoundation\Session\Session;
    /** @var Session $session */
    $session = $request->getSession();

@jderusse
Copy link
Member

wait.. nothing told that $this->get('session') returns an instance of Session. So, replacing $this->get('session'); // returns mixed by $request->getSession() //returns SessionInterface is absolutely identical. In both case you can't be sure that the returned object will be an instance of Session but in both case you can be confident enough, that in the context of your application you'll get an instance of Session.

This issue is not related to Session deprecation (has the behavior remains the same). And is a duplicate of: #39222, #20258, #11279, #10036, #5568, #39222

@shyim
Copy link
Contributor Author

shyim commented Jun 29, 2021

But the deprecation will remove also session.flash_bag from the DI. So it's related for me :D .

@nicodemuz
Copy link
Contributor

Thumbs up! I'm also getting an error on Psalm for this:

ERROR: UndefinedInterfaceMethod - src/Helper/FlashMessageHelper.php:34:25 - Method Symfony\Component\HttpFoundation\Session\SessionInterface::getFlashBag does not exist (see https://psalm.dev/181)
        $this->session->getFlashBag()->add('success', $message);

@ruudk
Copy link
Contributor

ruudk commented Jan 11, 2022

What about creating a WithFlashBag interface that holds public function getFlashBag(); that can used as an intersection type on PHP 8.1: SessionInterface&WithFlashBag

@fabpot fabpot closed this as completed in 077d4c9 Jul 3, 2022
symfony-splitter pushed a commit to symfony/http-foundation that referenced this issue Jul 3, 2022
This PR was squashed before being merged into the 6.2 branch.

Discussion
----------

Introduce FlashBagAwareSessionInterface

| Q             | A
| ------------- | ---
| Branch?       | 6.2 for features
| Bug fix?      | no
| New feature?  | yes
| Deprecations? | no
| Tickets       | Fix #41765
| License       | MIT
| Doc PR        | symfony/symfony-docs#... Not needed

This solve symfony/symfony#41765

People will be able to write
```
$session = $request->getSession();
if ($session instanceof FlashBagAwareSessionInterface) {
     $session->addFlashBag(...);
}
```

This is better than a `instanceof Session` check (which is not working with decoration).

Commits
-------

026c748208 Introduce FlashBagAwareSessionInterface
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants