Skip to content

Adding warning about depending on concrete implementation of Session #13884

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

Conversation

oleg-andreyev
Copy link
Contributor

@oleg-andreyev oleg-andreyev commented Jun 22, 2020

Relying on the concrete implementation of any class goes against best practices.
So if the application relies on SessionInterface user will find themself in a situation when "getFlashBag" is not declared in a SessionInterface exactly for this case I've added a warning about using SessionInterface and FlashBagInterface

Example of an issue when concrete Session class is used sonata-project/SonataUserBundle#1174
Example of an issue when SessionInterface is not enough: sonata-project/SonataUserBundle#1184

@javiereguiluz
Copy link
Member

Oleg, your proposed content is perfectly fine technically speaking. Thanks for that!

However, there's a problem. This controller.rst article is one of the critical Symfony Docs articles. It's part of the "Getting Started" series, which is recommended to all Symfony newcomers who want to learn the basics. The problem is that your suggestion is way too advanced for an article of that type. Moreover, this is really "a Symfony problem" which should be fixed ... so I'd say we shouldn't add this to the docs (even if it's perfectly correct).

@symfony/team-symfony-docs what do you think? Thanks!

@oleg-andreyev
Copy link
Contributor Author

@javiereguiluz Any updates on this?

@wouterj
Copy link
Member

wouterj commented Aug 22, 2020

My personal opinion would be that the current state of this doc is probably the best.. I agree that it's not good to rely on an implementation, but sadly for session you sort of have to (as you also explained in the note). This is one of the few bad designs left in Symfony 5 and there are some ideas to fix it (See e.g. symfony/symfony#10557 ).

However, until it's fixed, relying on the Session implementation is the best way to get the session imho.

@@ -534,6 +534,14 @@ After processing the request, the controller sets a flash message in the session
and then redirects. The message key (``notice`` in this example) can be anything:
you'll use this key to retrieve the message.

.. warning::

If you want avoid depending on concrete implementation of `Session` and rely on :class:`Symfony\\Component\\HttpFoundation\\Session\\SessionInterface`:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
If you want avoid depending on concrete implementation of `Session` and rely on :class:`Symfony\\Component\\HttpFoundation\\Session\\SessionInterface`:
If you want avoid depending on concrete implementation of ``Session`` and rely on :class:`Symfony\\Component\\HttpFoundation\\Session\\SessionInterface`:

.. warning::

If you want avoid depending on concrete implementation of `Session` and rely on :class:`Symfony\\Component\\HttpFoundation\\Session\\SessionInterface`:
you will find your self in a situation that `getFlashBag` is not declared in an interface.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
you will find your self in a situation that `getFlashBag` is not declared in an interface.
you will find your self in a situation that ``getFlashBag`` is not declared in an interface.

If you want avoid depending on concrete implementation of `Session` and rely on :class:`Symfony\\Component\\HttpFoundation\\Session\\SessionInterface`:
you will find your self in a situation that `getFlashBag` is not declared in an interface.
In order fix this "problem" you must inject `FlashBagInterface`/`session.flash_bag` and relay on `FlashBagInterface`
instead of `Session` for access to "Flash Messages"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
instead of `Session` for access to "Flash Messages"
instead of ``Session`` for access to "Flash Messages"


If you want avoid depending on concrete implementation of `Session` and rely on :class:`Symfony\\Component\\HttpFoundation\\Session\\SessionInterface`:
you will find your self in a situation that `getFlashBag` is not declared in an interface.
In order fix this "problem" you must inject `FlashBagInterface`/`session.flash_bag` and relay on `FlashBagInterface`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
In order fix this "problem" you must inject `FlashBagInterface`/`session.flash_bag` and relay on `FlashBagInterface`
In order fix this "problem" you must inject ``FlashBagInterface``/``session.flash_bag`` and relay on ``FlashBagInterface``

@javiereguiluz
Copy link
Member

I'm closing this because of the reasons given above. Moreover, the SessionInterface is deprecated as of symfony/symfony#38616 and we've updated the docs with all the session-related changes (see #14898).

In any case, thanks for this proposal and for taking care of reviewing/improving docs. Thanks!

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

Successfully merging this pull request may close these issues.

6 participants