Skip to content

[Session] Update configurations #15378

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

Merged
merged 1 commit into from
May 27, 2021
Merged
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
39 changes: 33 additions & 6 deletions components/http_foundation/session_configuration.rst
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ All native save handlers are internal to PHP and as such, have no public facing
They must be configured by ``php.ini`` directives, usually ``session.save_path`` and
potentially other driver specific directives. Specific details can be found in
the docblock of the ``setOptions()`` method of each class. For instance, the one
provided by the Memcached extension can be found on :phpmethod:`php.net <Memcached::setOption>`.
provided by the Memcached extension can be found on :phpmethod:`php.net <Memcached::setOptions>`.

While native save handlers can be activated by directly using
``ini_set('session.save_handler', $name);``, Symfony provides a convenient way to
Expand Down Expand Up @@ -170,12 +170,39 @@ collection. That's why Symfony now overwrites this value to ``1``.
If you wish to use the original value set in your ``php.ini``, add the following
configuration:

.. code-block:: yaml
.. configuration-block::

# config/packages/framework.yaml
framework:
session:
gc_probability: null
.. code-block:: yaml

# config/packages/framework.yaml
framework:
session:
gc_probability: null

.. 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 gc_probability="null"/>
</framework:config>
</container>

.. code-block:: php

// config/packages/framework.php
$container->loadFromExtension('framework', [
'session' => [
'gc_probability' => null,
],
]);

You can configure these settings by passing ``gc_probability``, ``gc_divisor``
and ``gc_maxlifetime`` in an array to the constructor of
Expand Down
44 changes: 44 additions & 0 deletions session.rst
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,50 @@ the default ``AttributeBag`` by the ``NamespacedAttributeBag``:
session.namespacedattributebag:
class: Symfony\Component\HttpFoundation\Session\Attribute\NamespacedAttributeBag

.. code-block:: xml

<!-- config/services.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"
xsi:schemaLocation="http://symfony.com/schema/dic/services https://symfony.com/schema/dic/services/services-1.0.xsd">

<services>
<service id="session" class="Symfony\Component\HttpFoundation\Session\Session" public="true">
<argument type="service" id="session.storage"/>
<argument type="service" id="session.namespacedattributebag"/>
<argument type="service" id="session.flash_bag"/>
</service>

<service id="session.namespacedattributebag"
class="Symfony\Component\HttpFoundation\Session\Attribute\NamespacedAttributeBag"
/>
</services>
</container>

.. code-block:: php

// config/services.php
namespace Symfony\Component\DependencyInjection\Loader\Configurator;

use Symfony\Component\HttpFoundation\Session\Attribute\NamespacedAttributeBag;
use Symfony\Component\HttpFoundation\Session\Session;

return function(ContainerConfigurator $configurator) {
$services = $configurator->services();

$services->set('session', Session::class)
->public()
->args([
ref('session.storage'),
ref('session.namespacedattributebag'),
ref('session.flash_bag'),
])
;

$services->set('session.namespacedattributebag', NamespacedAttributeBag::class);
};

.. _session-avoid-start:

Avoid Starting Sessions for Anonymous Users
Expand Down
12 changes: 6 additions & 6 deletions session/database.rst
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,6 @@ First, define a Symfony service for the connection to the Redis server:

.. code-block:: php

use Symfony\Component\DependencyInjection\Reference;

// ...
$container
// you can also use \RedisArray, \RedisCluster or \Predis\Client classes
Expand All @@ -90,7 +88,7 @@ and ``RedisProxy``:
arguments:
- '@Redis'
# you can optionally pass an array of options. The only options are 'prefix' and 'ttl',
# which define the prefix to use for the keys to avoid collision on the Redis server
# which define the prefix to use for the keys to avoid collision on the Redis server
# and the expiration time for any given entry (in seconds), defaults are 'sf_s' and null:
# - { 'prefix': 'my_prefix', 'ttl': 600 }

Expand All @@ -101,25 +99,27 @@ and ``RedisProxy``:
<service id="Symfony\Component\HttpFoundation\Session\Storage\Handler\RedisSessionHandler">
<argument type="service" id="Redis"/>
<!-- you can optionally pass an array of options. The only options are 'prefix' and 'ttl',
which define the prefix to use for the keys to avoid collision on the Redis server
which define the prefix to use for the keys to avoid collision on the Redis server
and the expiration time for any given entry (in seconds), defaults are 'sf_s' and null:
<argument type="collection">
<argument key="prefix">my_prefix</argument>
<argument key="ttl">600</argument>
</argument> -->
</service>
</services>

.. code-block:: php

// config/services.php
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\HttpFoundation\Session\Storage\Handler\RedisSessionHandler;

$container
->register(RedisSessionHandler::class)
->addArgument(
new Reference('Redis'),
// you can optionally pass an array of options. The only options are 'prefix' and 'ttl',
// which define the prefix to use for the keys to avoid collision on the Redis server
// which define the prefix to use for the keys to avoid collision on the Redis server
// and the expiration time for any given entry (in seconds), defaults are 'sf_s' and null:
// ['prefix' => 'my_prefix', 'ttl' => 600],
);
Expand Down Expand Up @@ -208,7 +208,7 @@ first register a new handler service with your database credentials:
<argument>%env(DATABASE_URL)%</argument>

<!-- you can also use PDO configuration, but requires passing two arguments: -->
<!-- <argument>mysql:dbname=mydatabase, host=myhost</argument>
<!-- <argument>mysql:dbname=mydatabase; host=myhost; port=myport</argument>
<argument type="collection">
<argument key="db_username">myuser</argument>
<argument key="db_password">mypassword</argument>
Expand Down