Skip to content

[FrameworkBundle] Add documentation about using a DSN as the session.handler_id #15560

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
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 38 additions & 3 deletions reference/configuration/framework.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1364,10 +1364,45 @@ handler_id

**type**: ``string`` **default**: ``'session.handler.native_file'``

The service id used for session storage. The default value ``'session.handler.native_file'``
The service id or DSN used for session storage. The default value ``'session.handler.native_file'``
will let Symfony manage the sessions itself using files to store the session metadata.
Set it to ``null`` to use the native PHP session mechanism.
You can also :doc:`store sessions in a database </session/database>`.
Set it to ``null`` to use the native PHP session mechanism. You can also provide a DSN to specify
the used storage-directory or :doc:`store sessions in a database </session/database>`:

.. configuration-block::

.. code-block:: yaml

# config/packages/framework.yaml
framework:
session:
# ...
handler_id: 'file://%kernel.project_dir%/var/sessions'
Copy link
Contributor

Choose a reason for hiding this comment

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


.. code-block:: xml

<!-- config/packages/framework.xml -->
<?xml version="1.0" encoding="UTF-8" ?>
<container xmlns="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>
<framework:session enabled="true" handler-id="file://%kernel.project_dir%/var/sessions"/>
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
<framework:session enabled="true" handler-id="file://%kernel.project_dir%/var/sessions"/>
<framework:session enabled="true"
handler-id="file://%kernel.project_dir%/var/sessions"
/>

</framework:config>
</container>

.. code-block:: php

// config/packages/framework.php
$container->loadFromExtension('framework', [
Copy link
Contributor

Choose a reason for hiding this comment

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

Should use the container configurator as done in other config blocks.

'session' => [
// ...
'handler_id' => 'file://%kernel.project_dir%/var/sessions',
],
]);

.. _name:

Expand Down
58 changes: 58 additions & 0 deletions session/database.rst
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,64 @@ across different servers.
Symfony can store sessions in all kinds of databases (relational, NoSQL and
key-value) but recommends key-value databases like Redis to get best performance.

Define Session Storage by DSN
Copy link
Contributor

Choose a reason for hiding this comment

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

Given my comment above this section should not be necessary.

-----------------------------

Besides specifying a service as the session-handler you can also provide a
"DSN" directly and let Symfony create the necessary services/clients:

.. configuration-block::

.. code-block:: yaml

# config/packages/framework.yaml
framework:
session:
# ...
handler_id: 'redis://password@redis-server:6379'

.. code-block:: xml

<!-- config/packages/framework.xml -->
<?xml version="1.0" encoding="UTF-8" ?>
<container xmlns="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>
<framework:session enabled="true" handler-id="redis://password@redis-server:6379"/>
</framework:config>
</container>

.. code-block:: php

// config/packages/framework.php
$container->loadFromExtension('framework', [
'session' => [
// ...
'handler_id' => 'redis://password@redis-server:6379',
],
]);

Supported DSN protocols are:
Copy link
Contributor

Choose a reason for hiding this comment

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

I would move this part in a .. note:: directive under the first example if this section were to be removed.


- file (i.e.: ``file://%kernel.project_dir%/var/sessions``)
- redis (i.e.: ``redis://redis-server:6379``)
- rediss
- memcached
- pdo_oci
- mssql
- mysql
- mysql2
- pgsql
- postgres
- postgresql
- sqlsrv
- sqlite
- sqlite3

Store Sessions in a key-value Database (Redis)
----------------------------------------------

Expand Down