D
D
Dimd132016-08-29 10:08:18
symfony
Dimd13, 2016-08-29 10:08:18

Symfony2.8 Sessions in MongoDB. How to set up correctly?

Hello.
For a project on Symfony (v2.8.9) I'm trying to set up session storage in MongoDB. The server has PHP7.0.8 installed. The mongodb driver is installed via pecl and extension=mongodb.so is added to php.ini
sudo pecl install mongodb
I configure Symfony as described in the " How to Use MongoDbSessionHandler to Store Sessions... " document, substituting my data for connecting to the database

# app/config/config.yml
framework:
    session:
        # ...
        handler_id:  session.handler.mongo
        cookie_lifetime: 2592000 # optional, it is set to 30 days here
        gc_maxlifetime: 2592000 # optional, it is set to 30 days here

services:
    # ...
    mongo_client:
        class: MongoClient
        # if using a username and password
        arguments: ['mongodb://%mongodb_username%:%mongodb_password%@%mongodb_host%:27017']
        
    session.handler.mongo:
        class: Symfony\Component\HttpFoundation\Session\Storage\Handler\MongoDbSessionHandler
        arguments: ['@mongo_client', '%mongo.session.options%']

# app/config/parameters.yml
parameters:
    # ...
    mongo.session.options:
        database: session_db # your MongoDB database name
        collection: session  # your MongoDB collection name
    mongodb_host: 1.2.3.4 # your MongoDB server's IP
    mongodb_username: my_username
    mongodb_password: my_password

In response to a request to the server, I get an error
ClassNotFoundException in appDevDebugProjectContainer.php line 2232:
Attempted to load class "MongoClient" from the global namespace.
Did you forget a "use" statement?

I tried to put MongoDB\Driver\Manager instead of the MongoClient class, it fails with an error
ClassNotFoundException in appDevDebugProjectContainer.php line 2232:
Attempted to load class "Manager" from namespace "MongoDB\Driver".
Did you forget a "use" statement for another namespace?

Could you tell me what could be the reason for the error and how to overcome it?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
B
BoShurik, 2016-08-29
@Dimd13

Probably, the information in the dock refers to the old driver. Add dependencies:

The Mongo PHP Adapter is a userland library designed to act as an adapter between applications relying on ext-mongo and the new driver (ext-mongodb).
It provides the API of ext-mongo built on top of mongo-php-library, thus being compatible with PHP 7.

Mongo PHP Adapter

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question