Skip to content

Document host parameter and external parameters in "How to Use PdoSessionHandler..." #9493

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
Changes from 2 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
49 changes: 31 additions & 18 deletions doctrine/pdo_session_storage.rst
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ To use it, first register a new handler service:

Symfony\Component\HttpFoundation\Session\Storage\Handler\PdoSessionHandler:
arguments:
- 'mysql:dbname=mydatabase'
- { db_username: myuser, db_password: mypassword }
- 'mysql:dbname=%env(MYDATABASE_NAME)%, host=%env(MYDATABASE_HOST)%'
- { db_username: '%env(MYDATABASE_USERNAME)%', db_password: '%env(MYDATABASE_PASSWORD)%' }

# If you're using Doctrine & want to re-use that connection, then:
# comment-out the above 2 lines and uncomment the line below
Expand All @@ -43,10 +43,10 @@ To use it, first register a new handler service:

<services>
<service id="Symfony\Component\HttpFoundation\Session\Storage\Handler\PdoSessionHandler" public="false">
<argument>mysql:dbname=mydatabase</argument>
<argument>mysql:dbname=%env(MYDATABASE_NAME)%, host=%env(MYDATABASE_HOST)%</argument>
<argument type="collection">
<argument key="db_username">myuser</argument>
<argument key="db_password">mypassword</argument>
<argument key="db_username">%env(MYDATABASE_USERNAME)%</argument>
<argument key="db_password">%env(MYDATABASE_PASSWORD)%</argument>
</argument>
</service>
</services>
Expand All @@ -59,11 +59,19 @@ To use it, first register a new handler service:

$storageDefinition = $container->autowire(PdoSessionHandler::class)
->setArguments(array(
'mysql:dbname=mydatabase',
array('db_username' => 'myuser', 'db_password' => 'mypassword')
'mysql:dbname=%env(MYDATABASE_NAME)%, host=%env(MYDATABASE_HOST)%',
array('db_username' => '%env(MYDATABASE_USERNAME)%', 'db_password' => '%env(MYDATABASE_PASSWORD)')
))
;

.. note::

Ideally you want to reuse your database information from your environment variables.
These variables can be referenced in the service configuration using ``%env(PARAMETER_NAME)%``.
Do not forget to wrap these with single or double quotes in yaml.

See :doc:`/configuration/external_parameters`.

Next, tell Symfony to use your service as the session handler:

.. configuration-block::
Expand Down Expand Up @@ -115,8 +123,8 @@ a second array argument to ``PdoSessionHandler``:

Symfony\Component\HttpFoundation\Session\Storage\Handler\PdoSessionHandler:
arguments:
- 'mysql:dbname=mydatabase'
- { db_table: sessions, db_username: myuser, db_password: mypassword }
- 'mysql:dbname=%env(MYDATABASE_NAME)%, host=%env(MYDATABASE_HOST)%'
- { db_table: '%env(SESSIONS_TABLE)%, db_username: '%env(MYDATABASE_USERNAME)%', db_password: '%env(MYDATABASE_PASSWORD)%' }

.. code-block:: xml

Expand All @@ -129,11 +137,11 @@ a second array argument to ``PdoSessionHandler``:

<services>
<service id="Symfony\Component\HttpFoundation\Session\Storage\Handler\PdoSessionHandler" public="false">
<argument>mysql:dbname=mydatabase</argument>
<argument>mysql:dbname=%env(MYDATABASE_NAME)%, host=%env(MYDATABASE_HOST)%</argument>
<argument type="collection">
<argument key="db_table">sessions</argument>
<argument key="db_username">myuser</argument>
<argument key="db_password">mypassword</argument>
<argument key="db_username">%env(MYDATABASE_USERNAME)%</argument>
<argument key="db_password">%env(MYDATABASE_PASSWORD)%</argument>
<argument key="db_table">%env(SESSIONS_TABLE)%</argument>
</argument>
</service>
</services>
Expand All @@ -147,11 +155,16 @@ a second array argument to ``PdoSessionHandler``:
// ...

$container->autowire(PdoSessionHandler::class)
->setArguments(array(
'mysql:dbname=mydatabase',
array('db_table' => 'sessions', 'db_username' => 'myuser', 'db_password' => 'mypassword')
))
;
->setArguments(
array(
'mysql:dbname=%env(MYDATABASE_NAME)%, host=%env(MYDATABASE_HOST)%',
array(
'db_table' => '%env(SESSIONS_TABLE)%',
'db_username' => '%env(MYDATABASE_USERNAME)%',
'db_password' => '%env(MYDATABASE_PASSWORD)'
Copy link
Contributor

Choose a reason for hiding this comment

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

The trailing comma in the last element of the arrays is missing, look #8325

Copy link
Contributor Author

@pueppiblue pueppiblue Apr 24, 2018

Choose a reason for hiding this comment

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

$container->autowire(PdoSessionHandler::class) 
            ->setArguments( \
                array( \
                    'mysql:dbname=%env(MYDATABASE_NAME)%, host=%env(MYDATABASE_HOST)%',
                    array(
                        'db_table' => '%env(SESSIONS_TABLE)%',
                        'db_username' => '%env(MYDATABASE_USERNAME)%',
                        'db_password' => '%env(MYDATABASE_PASSWORD)',
                    ), // awkward comma
                )
            );

To be really consistent, I'd to add another comma, but for me it looks kind of awkward after the closing brace.
What would you think?

)
)
);

These are parameters that you can configure:

Expand Down