New in Symfony 5.3: Session Service Deprecation

Contributed by Jérémy Derussé in #38616.

In Symfony applications you can access the session via the session service or its SessionInterface autowiring alias. This is convenient, but it’s technically wrong for some reasons:

Session is a data object (e.g. like the Request object) so there shouldn’t be a service defined for it in the container; Sessions are not part of the HTTP specification (either HTTP/1.1, HTTP/2 or HTTP/3) because HTTP is stateless. That’s why it feels odd to handle sessions as part of the HttpFoundation component.

That’s why, many years after it was first proposed, we’re deprecating the session service in Symfony 5.3 and we’ll remove it in Symfony 6.0. Instead of injecting that session service you now have to inject the RequestStack service and use the new getSession() method: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19use Symfony\Component\HttpFoundation\RequestStack;

class SomeService { private $requestStack;

public function __construct(RequestStack $requestStack)
{
    $this->requestStack = $requestStack;
}

public function someMethod()
{
    $session = $this->requestStack->getSession();
    // ... do something with the session
}

// ...

}

                Sponsor the Symfony project.

http://feedproxy.google.com/~r/symfony/blog/~3/OqWZSGvz3z4/new-in-symfony-5-3-session-service-deprecation

Létrehozva 4y | 2021. máj. 5. 7:20:07


Jelentkezéshez jelentkezzen be

EGYÉB POSTS Ebben a csoportban

SymfonyOnline June 2025: FormFlow: Build Stunning Multistep Forms

SymfonyOnline June 2025 is almost here, starting in almost 2 months on:

June 10-11: Workshop days. It is possible to attend 1 two-day training or 2 one-day trainings. June 12-13: Online confe

2025. ápr. 23. 15:20:21 | Symfony
New in Symfony 7.3: Assets Pre-Compression

Contributed by Kévin Dunglas in

2025. ápr. 23. 8:20:31 | Symfony
SymfonyOnline June 2025: Inside a Financial App Breach: Debugging a Million-Dollar Bug

SymfonyOnline June 2025 is almost here, starting in almost 2 months on:

June 10-11: Workshop days. It is possible to attend 1 two-day training or 2 one-day trainings. June 12-13: Online confe

2025. ápr. 22. 13:50:03 | Symfony
New in Symfony 7.3: Invokable Commands and Input Attributes

This is the first article in a series showcasing the most important new features introduced by Symfony 7.3, which will be released at the end of May 2025.

2025. ápr. 22. 9:10:36 | Symfony
A Week of Symfony #955 (April 14–20, 2025)

This week, the upcoming Symfony 7.3 version improved the AsAlias attribute by adding a new argument, introduced Clock support for UriSigner, and refined the return type of the ContainerInterface::get(

2025. ápr. 20. 8:30:06 | Symfony
SymfonyOnline June 2025: Rethinking File Handling in Symfony

SymfonyOnline June 2025 is almost here, starting in almost 2 months on:

June 10-11: Workshop days. It is possible to attend 1 two-day training or 2 one-day trainings. June 12-13: Online confe

2025. ápr. 16. 16:30:02 | Symfony
SymfonyLive Paris 2025: Recap and replay!

SymfonyLive Paris 2025 took place three weeks ago — a big thank you to everyone who joined us! The conference was held entirely in French, and now you can relive the best moments: replays, hig

2025. ápr. 15. 14:50:24 | Symfony