Skip to content

Commit 56d26cf

Browse files
[FrameworkBundle] Add documentation about using a DSN as the session.handler_id
1 parent e886b53 commit 56d26cf

File tree

1 file changed

+70
-2
lines changed

1 file changed

+70
-2
lines changed

reference/configuration/framework.rst

Lines changed: 70 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1608,10 +1608,78 @@ handler_id
16081608

16091609
**type**: ``string`` **default**: ``session.handler.native_file``
16101610

1611-
The service id used for session storage. The default value ``'session.handler.native_file'``
1611+
The service id or DSN used for session storage. The default value ``'session.handler.native_file'``
16121612
will let Symfony manage the sessions itself using files to store the session metadata.
16131613
Set it to ``null`` to use the native PHP session mechanism.
1614-
You can also :ref:`store sessions in a database <session-database>`.
1614+
It is possible to :ref:`store sessions in a database <session-database>`,
1615+
and also to configure the session handler with a DSN:
1616+
1617+
.. configuration-block::
1618+
1619+
.. code-block:: yaml
1620+
# config/packages/framework.yaml
1621+
framework:
1622+
session:
1623+
# ...
1624+
handler_id: 'redis://localhost'
1625+
handler_id: '%env(REDIS_URL)%'
1626+
handler_id: '%env(DATABASE_URL)%'
1627+
handler_id: 'file://%kernel.project_dir%/var/sessions'
1628+
1629+
.. code-block:: xml
1630+
<!-- config/packages/framework.xml -->
1631+
<?xml version="1.0" encoding="UTF-8" ?>
1632+
<container xmlns="http://symfony.com/schema/dic/services"
1633+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
1634+
xmlns:framework="http://symfony.com/schema/dic/symfony"
1635+
xsi:schemaLocation="http://symfony.com/schema/dic/services
1636+
https://symfony.com/schema/dic/services/services-1.0.xsd
1637+
http://symfony.com/schema/dic/symfony https://symfony.com/schema/dic/symfony/symfony-1.0.xsd">
1638+
<framework:config>
1639+
<!-- ... -->
1640+
<framework:session enabled="true"
1641+
handler-id="redis://localhost"
1642+
handler-id="%env(REDIS_URL)%"
1643+
handler-id="%env(DATABASE_URL)%"
1644+
handler-id="file://%kernel.project_dir%/var/sessions"/>
1645+
</framework:config>
1646+
</container>
1647+
1648+
.. code-block:: php
1649+
1650+
// config/packages/framework.php
1651+
use Symfony\Config\FrameworkConfig;
1652+
use function Symfony\Component\DependencyInjection\Loader\Configurator\env;
1653+
1654+
return static function (FrameworkConfig $framework): void {
1655+
// ...
1656+
1657+
$framework->session()
1658+
// ...
1659+
->handlerId('redis://localhost')
1660+
->handlerId(env('REDIS_URL'))
1661+
->handlerId(env('DATABASE_URL'))
1662+
->handlerId('file://%kernel.project_dir%/var/sessions');
1663+
};
1664+
1665+
.. note::
1666+
1667+
Supported DSN protocols are the following:
1668+
1669+
* ``file``
1670+
* ``redis``
1671+
* ``rediss`` (Redis over TLS)
1672+
* ``memcached`` (requires :doc:`symfony/cache </components/cache>`)
1673+
* ``pdo_oci`` (requires :doc:`doctrine/dbal </doctrine/dbal>`)
1674+
* ``mssql``
1675+
* ``mysql``
1676+
* ``mysql2``
1677+
* ``pgsql``
1678+
* ``postgres``
1679+
* ``postgresql``
1680+
* ``sqlsrv``
1681+
* ``sqlite``
1682+
* ``sqlite3``
16151683

16161684
.. _name:
16171685

0 commit comments

Comments
 (0)