By default Symfony stores session data using the default handler, which uses the server filesystem.

Storing session data in files has many drawbacks and in many cases it just does not even work, with simplest example being if your app is served by multiple web servers behind a load balancer.

If you are already using MongoDB to store your application data using Doctrine ODM, it makes a lot of sense to use MongoDB as well for sessions.

The configuration is quite simple thanks to the Dependency Injection Container (DIC) and the flexible configuration of Symfony. The MongoDbSessionHandler class requires a reference to the Mongo object (the connection to MongoDB) and an array with a few entries to define which database and collection to use.

First, we need to define the session handler service in the services section of config.yml:

Since you are already using Doctrine ODM, it is a good idea to reuse the connection for your sessions. This is easily accomplished by defining a couple of extra services in the same section where you added the previous entry:

Upcoming Conferences

Nice! In an example of how to use mongodb to avoid problems when using a load balancer, you should probably mention how to specify the mongodb host.

Morgan

Hello,
Nice work.

I wonder if this solution would solve my problem. Let me explain. I have several domain example:
main-domain.com
site.client1.com
site.client2.com
…
I would share the sessions on all these areas. They are on the same server. Is this possible with this solution? I set up mongo session storage, it works but not sharing.