Sessions are provided by the HttpFoundation component, which is included in
all Symfony applications, no matter how you installed it. Before using the
sessions, check their default configuration:

YAML

1
2
3
4
5
6
7
8
9
10
11

# config/packages/framework.yamlframework:session:# enables the support of sessions in the appenabled:true# ID of the service used for session storage.# NULL means that Symfony uses PHP default session mechanismhandler_id:null# improves the security of the cookies used for sessionscookie_secure:'auto'cookie_samesite:'lax'

XML

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

<!-- config/packages/framework.xml --><?xml version="1.0" encoding="UTF-8" ?><containerxmlns="http://symfony.com/schema/dic/services"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:framework="http://symfony.com/schema/dic/symfony"xsi:schemaLocation="http://symfony.com/schema/dic/services https://symfony.com/schema/dic/services/services-1.0.xsd http://symfony.com/schema/dic/symfony https://symfony.com/schema/dic/symfony/symfony-1.0.xsd"><framework:config><!-- enabled: enables the support of sessions in the app handler-id: ID of the service used for session storage NULL means that Symfony uses PHP default session mechanism cookie-secure and cookie-samesite: improves the security of the cookies used for sessions --><framework:sessionenabled="true"handler-id="null"cookie-secure="auto"cookie-samesite="lax"/></framework:config></container>

PHP

1
2
3
4
5
6
7
8
9
10
11
12
13

// config/packages/framework.php$container->loadFromExtension('framework',['session'=>[// enables the support of sessions in the app'enabled'=>true,// ID of the service used for session storage// NULL means that Symfony uses PHP default session mechanism'handler_id'=>null,// improves the security of the cookies used for sessions'cookie_secure'=>'auto','cookie_samesite'=>'lax',],]);

Setting the handler_id config option to null means that Symfony will
use the native PHP session mechanism. The session metadata files will be stored
outside of the Symfony application, in a directory controlled by PHP. Although
this usually simplify things, some session expiration related options may not
work as expected if other applications that write to the same directory have
short max lifetime settings.

If you prefer, you can use the session.handler.native_file service as
handler_id to let Symfony manage the sessions itself. Another useful option
is save_path, which defines the directory where Symfony will store the
session metadata files:

Symfony provides a session service that is injected in your services and
controllers if you type-hint an argument with
SessionInterface:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25

useSymfony\Component\HttpFoundation\Session\SessionInterface;classSomeService{private$session;publicfunction__construct(SessionInterface$session){$this->session=$session;}publicfunctionsomeMethod(){// stores an attribute in the session for later reuse$this->session->set('attribute-name','attribute-value');// gets an attribute by name$foo=$this->session->get('foo');// the second argument is the value returned when the attribute doesn't exist$filters=$this->session->get('filters',[]);// ...}}

Tip

Every SessionInterface implementation is supported. If you have your
own implementation, type-hint this in the argument instead.

Stored attributes remain in the session for the remainder of that user's session.
By default, session attributes are key-value pairs managed with the
AttributeBag
class.

If your application needs are complex, you may prefer to use
namespaced session attributes which are managed with the
NamespacedAttributeBag
class. Before using them, override the session service definition to replace
the default AttributeBag by the NamespacedAttributeBag:

Sessions are automatically started whenever you read, write or even check for
the existence of data in the session. This may hurt your application performance
because all users will receive a session cookie. In order to prevent that, you
must completely avoid accessing the session.

For example, if your templates include some code to display the
flash messages, sessions will start even if the user
is not logged in and even if you haven't created any flash messages. To avoid
this behavior, add a check before trying to access the flash messages:

1
2
3
4
5
6
7
8

{# this check prevents starting a session when there are no flash messages #}{%ifapp.request.hasPreviousSession%}{%formessageinapp.flashes('notice')%}<divclass="flash-notice">{{message}}</div>{%endfor%}{%endif%}