-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[5.0][HttpFoundation] The session must not be a service #10557
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
Comments
👍 |
Or a least, the scope (if still exists), should be |
why would you have a different session for the subrequest ? |
Did not think about subrequest :(
I think I prefer the second one. |
what would be the application scope ? |
We don't need a new scope... as I really want to get rid of them in 3.0. So, the session is just an object embedded into the request, and as such, it is already available through the request stack. There is not need for yet another abstraction. The only thing we need to do is to deprecate the session as a service. |
@fabpot This might require refactoring the way the session is handled though. session storages look like services. thus, if we consider that the main access point for the session is through the request, it is a bit weird because the session is injected into the request later (and has a wider scope than the request as it is shared in subrequests) |
I have mixed feelings with this. As @stof said, the session (as a concept itself) is not only a data object, it has persistence and there are many use cases where you would want (or at least it is convenient to have) a session service injected in other services In fact, for instance, the main class in the facebook-php-sdk expects a session object to be injected in order to handle your auth with Facebook in the subsequent requests. And this is used by people creating Facebook apps/videogames with Symfony2 So, if we go via scopes, it becomes a mess (it has more or less been rejected as per @fabpot comment) and I am not sure if deprecating it as a service is convenient at all. Just my 2 cents, of course :) |
Whatever we do, I don't think we should bind it to request_stack or the Request object. The session is way more than the session provided by the Request workflow. |
It is not possible to have different sessions for subrequests. |
I'm sharing my thought as it comes.
What about a data mapper impl. of session storage ? This way the original idea of data object is kept. |
@docteurklein 👍 for a distinction between a session storage service and a session value object |
@docteurklein can you explain a little more about "The session knows too much about how to store itself." |
@docteurklein your third point is abit problematic IMO because we are not totally free for the way the session system works. It depends on the PHP session system |
@stof if you talk about 5.4's native handlers & co., I think it could be handled the same way as currently, but delegated to the data mapper. @Drak to be honest, I have nothing against the way session works currently :) I just try to give potential solutions to the problem of session as a data object. Just replace the term entity with the term "session object", and you have what I talked about. |
I think the session needs an overhaul in general. The SessionInterface along with the handlers and storage are too confusing when it comes to implementation. Imo the session is fine as service, but it's too stateful for that as it is. |
I think there should be strong distinction between a session storage (or a value object) and a session driver, they both should be inject-able, however the first one with very limited usage, maybe even read-only, and replaceable/editable only using a session driver. I can see a point of injecting different session into sub-requests, and it would be a definitely a robust feature, maybe not used often but still. I'm not sure how @fabpot will get rid of scopes, but I can see instances of services/events that are fired prior to session. |
I assume the OP is referring to the data access object pattern and I agree that it is stupid to put DAOs in the container. However, it is completely legit for container services to provide access to DAOs, i.e. provide methods returning those objects. The problem with the current In many cases a consumer of the session is interested in its DAO interface, i.e. the key-value collection. In those cases it is nice if the collection simply can be accessed using However, things get more complicated when working with session bags. Is it more appropriate to call Splitting up the #6840 proposed to move the session out of HttpFoundation into its own component. This would also lead to
Removing
|
This PR was merged into the 2.8 branch. Discussion ---------- [DependencyInjection] Deprecate scope concept | Q | A | ------------- | --- | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | yes | Tests pass? | yes | License | MIT This PR mark as deprecated the concept of scopes in the DI container. See #10557 (comment). Also adds a new `shared` flag to the service definitions in replacement of the `prototype` scope. Commits ------- 6c4a676 Add "shared" flag and deprecate scopes concept
I like to change values in the session in my own service. Should I always pass the current request as parameter? Or should I only pass the session ( As I understand I should not get |
What about adding |
@nicolas-grekas Initially, I thought about |
The Session is bound to your HTTP layer, so whenever you need it somewhere, you can always pass it as method argument if you really need to modify the session contents in another layer. If you really, really want to modify the session in listeners that do not have a direct access to the request, you can always retrieve it from the current request via the request stack. If the events do not contain the request object while being part of the request lifecycle, perhaps they can be added. As far as I can see, the Kernel events have a request object and thus a session though: https://symfony.com/doc/current/reference/events.html#kernel-events (given the session is set).
|
We should then ensure subrequests have the session, or tell about |
Considering there are enough work-arounds, it seems that that it can be deprecated as service. Worst case scenario, provide an alternative solution to show how to register the Session as service using a factory in the DIC. |
Yes, it's a shortcut, but a convenient one. That's why I'm in favor to add it to the request stack |
I like the |
If we remove the service, can we make it so that controller actions that type-hint for |
@derrabus What do you mean exactly? The |
@linaori No, but the |
So, I started working on this as I thought "it should not be that hard". But discovered the symfony/src/Symfony/Component/HttpKernel/EventListener/AbstractSessionListener.php Lines 48 to 66 in a07627e
So that means we deprecate the |
@wouterj Either way, the session needs to be initialized somehow. At the moment, the container does this and the objective (as I understand it) would be to move that initialization somewhere else. That listener could be the place we're looking for. Since the In Symfony 5, the Does that make sense? |
First thing to me is to split the This would enforce the session to be created by either the storage, or a new factory that may need the storage itself, to create the session object, fetch data from the storage, populate the session with storage data, return the session as a data object. BC and migration path would be achieved with new contracts, making the "old" session a WDYT? |
The first thing here would be to propose solutions about how this split would look like actually. As said previously, this is not easy, due to the fact that we wrap the PHP session handling, and so we are limited by what it allows. |
…services (William Arslett) This PR was squashed before being merged into the 5.1-dev branch. Discussion ---------- [FrameworkBundle] Deprecate flashbag and attributebag services | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | no | Deprecations? | yes | Tickets | Related to [#10557](#10557) | Related to PR | #36063 | License | MIT FlashBag and AttributeBag are data objects and so should not be available via the service container. The preferred method for accessing these objects is via `$session->getFlashBag()` or `$session->getAttributeBag()` Commits ------- f9b52fe [FrameworkBundle] Deprecate flashbag and attributebag services
RequestStack could be used as a factory and getSession the factory method to protect BC breaks while the session service is deprecated but not removed same as we did for the flash bag in #36063. The Session could be instantiated in the SessionListener when the master request is received and onKernelRequest is triggered as @derrabus suggests. The problem I can see with this is if a service which injects the session is instantiated by the service container before that onKernelRequest event maybe in another event handler for example. In this situation getMasterRequest will be null so it won't be possible to get the Session from it. Another approach might be to remove this behaviour from the SessionListener and instead make the RequestStack the owner of the session and set it's session object on each request that is pushed to it. That way we can rely on the Session being available to the service container at any point in the process including to all KernelEvents.
We also have to consider applications that might have overwritten the definition of the session service with their own definition. We could maybe add a compiler pass that detects if the session service definition is overwritten (by checking whether it is using RequestStack as a factory) and if so replaces the definition for SessionFactoryInterface with one that uses an implementation that delegates to the service container. That implementation could be deprecated from the start so that any applications that are overriding the default session service will get deprecation notices. Any application can override the default instantiation of the session, use their own class, register their own bags etc. by implementing their own SessionFactoryInterface. |
…service "session" (jderusse) This PR was merged into the 5.3-dev branch. Discussion ---------- [FrameworkBundle][HttpFoundation][Security] Deprecate service "session" | Q | A | ------------- | --- | Branch? | 5.x | Bug fix? | no | New feature? | no | Deprecations? | yes | Tickets | Fix #10557 and Fix #12839 | License | MIT | Doc PR | TODO This is a attempt to deprecate service `session` and `SessionInterface`. This PR replaces the `session` service by a `.session.do-not-use` service (used internally by Symfony) and make `session` a deprecated alias. In Symfony 6.0 we can remove the `session` service and replace the `SessionListener` by a Factory that build the session (instead of fetching it from container) This PR also add a short cut `RequestStack::getSession(): ?SessionInterface` For backward compatibility the `SessionListener` is replaced by `FactorySessionListener` **only when** the user don't override the service `session` (ping @wouterj ) TODO: - [x] Test many configuration and dependencies (ie. session disabled + csrf) - [x] ChangeLog and Upgrade - [x] fix tests Commits ------- 54acc00 Deprecat service "session"
The session is a data object, and so, like the request, must not be in the container.
The text was updated successfully, but these errors were encountered: