From b2e6e10e69a0f7c61c668f4ab8411cd90c964a49 Mon Sep 17 00:00:00 2001 From: Justus Klein Date: Tue, 27 Jul 2021 11:49:15 +0200 Subject: [PATCH 1/2] Add documentation about using a DSN as the session.handler_id --- reference/configuration/framework.rst | 51 ++++++++++++++++++-- session/database.rst | 68 +++++++++++++++++++++++++++ 2 files changed, 116 insertions(+), 3 deletions(-) diff --git a/reference/configuration/framework.rst b/reference/configuration/framework.rst index da817c5df39..5dc5b494601 100644 --- a/reference/configuration/framework.rst +++ b/reference/configuration/framework.rst @@ -1364,10 +1364,55 @@ 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 `. +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 `: + +.. configuration-block:: + + .. code-block:: yaml + + # config/packages/framework.yaml + framework: + session: + # ... + handler_id: 'redis://localhost' + handler_id: '%env(REDIS_URL)%' + handler_id: '%env(resolve:DATABASE_URL)%' + handler_id: 'file://%kernel.project_dir%/var/sessions' + + .. code-block:: xml + + + + + + + + + + .. code-block:: php + + // config/packages/framework.php + $container->loadFromExtension('framework', [ + 'session' => [ + // ... + 'handler_id' => 'redis://localhost', + 'handler_id' => '%env(REDIS_URL)%', + 'handler_id' => '%env(resolve:DATABASE_URL)%', + 'handler_id' => 'file://%kernel.project_dir%/var/sessions', + ], + ]); .. _name: diff --git a/session/database.rst b/session/database.rst index 56cd9e27384..4b7edd977cb 100644 --- a/session/database.rst +++ b/session/database.rst @@ -11,6 +11,74 @@ 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 +----------------------------- + +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://localhost' + handler_id: '%env(REDIS_URL)%' + handler_id: '%env(resolve:DATABASE_URL)%' + handler_id: 'file://%kernel.project_dir%/var/sessions' + + .. code-block:: xml + + + + + + + + + + .. code-block:: php + + // config/packages/framework.php + $container->loadFromExtension('framework', [ + 'session' => [ + // ... + 'handler_id' => 'redis://localhost', + 'handler_id' => '%env(REDIS_URL)%', + 'handler_id' => '%env(resolve:DATABASE_URL)%', + 'handler_id' => 'file://%kernel.project_dir%/var/sessions', + ], + ]); + +Supported DSN protocols are: + +- 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) ---------------------------------------------- From d091d8768a1fe6c05d12bc465337e63e1eee543f Mon Sep 17 00:00:00 2001 From: Justus Klein Date: Thu, 29 Jul 2021 13:19:19 +0200 Subject: [PATCH 2/2] fix config syntax --- reference/configuration/framework.rst | 12 +----------- session/database.rst | 16 +++------------- 2 files changed, 4 insertions(+), 24 deletions(-) diff --git a/reference/configuration/framework.rst b/reference/configuration/framework.rst index 5dc5b494601..c220ba2a78b 100644 --- a/reference/configuration/framework.rst +++ b/reference/configuration/framework.rst @@ -1377,9 +1377,6 @@ the used storage-directory or :doc:`store sessions in a database - + @@ -1407,9 +1400,6 @@ the used storage-directory or :doc:`store sessions in a database loadFromExtension('framework', [ 'session' => [ // ... - 'handler_id' => 'redis://localhost', - 'handler_id' => '%env(REDIS_URL)%', - 'handler_id' => '%env(resolve:DATABASE_URL)%', 'handler_id' => 'file://%kernel.project_dir%/var/sessions', ], ]); diff --git a/session/database.rst b/session/database.rst index 4b7edd977cb..fa6a1e41caf 100644 --- a/session/database.rst +++ b/session/database.rst @@ -25,10 +25,7 @@ Besides specifying a service as the session-handler you can also provide a framework: session: # ... - handler_id: 'redis://localhost' - handler_id: '%env(REDIS_URL)%' - handler_id: '%env(resolve:DATABASE_URL)%' - handler_id: 'file://%kernel.project_dir%/var/sessions' + handler_id: 'redis://password@redis-server:6379' .. code-block:: xml @@ -41,11 +38,7 @@ Besides specifying a service as the session-handler you can also provide a 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"> - + @@ -55,10 +48,7 @@ Besides specifying a service as the session-handler you can also provide a $container->loadFromExtension('framework', [ 'session' => [ // ... - 'handler_id' => 'redis://localhost', - 'handler_id' => '%env(REDIS_URL)%', - 'handler_id' => '%env(resolve:DATABASE_URL)%', - 'handler_id' => 'file://%kernel.project_dir%/var/sessions', + 'handler_id' => 'redis://password@redis-server:6379', ], ]);