How to Use MongoDB for Sessions in Symfony Application

The tutorial assumes that the project is using MongoDB and has everything configured (Doctrine ODM bundle, etc.).

Add the services to app/config/services.yml:

services:
    mongo:
        class: Mongo
        factory_service: mongo.connection
        factory_method: getMongo

    mongo.connection:
        class: MongoDoctrine\MongoDB\Connection
        factory_service: doctrine.odm.mongodb.document_manager
        factory_method: getConnection
        calls:
            - [initialize, []]

    mongo.session.handler:
        class: Symfony\Component\HttpFoundation\Session\Storage\Handler\MongoDbSessionHandler
        arguments: ["@mongo", "%mongo.session.options%"]

Add the parameters for session handler:

# app/config/parameters.yml.dist
parameters:
    mongo.session.options:
        database: "%mongodb_database%"
        collection: sessions

Change the session handler in app/config/config.yml:

framework:
    session:
        handler_id: mongo.session.handler

Now your sessions should be saved in the MongoDB database.

If you want to change how long the session is persisted and improve security, add the following code to your configuration:

framework:
    session:
        cookie_lifetime: 1209600 # 14 days
        cookie_httponly: true

You might also find "How to save Symfony logs to MongoDB" interesting.

Posted in: MongoDB, Symfony, Technical, Tutorials

Comments