How to install Symfony3 with MongoDB

Roberto Butti
3 min readOct 6, 2016

Would you like to install and use Symfony3 with MongoDB and PHP7?

A good starting point for installing Symfony3 and Mongo DB is the official tutorial on Symfony web site. I think that it is a good tutorial because it is very clear.

In order to use Symfony with MongoDB you need:

Doctrine/MongoDB Stack needed by Symfony3 and php7
  • Symfony 3 : obviously :-) ;
  • Symfony Doctrine MongoDB Bundle (doctrine/mongodb-odm-bundle): this bundle integrates the Doctrine2 MongoDB Object Document Mapper (ODM) library into Symfony;
  • Doctrine MongoDB Object Document Mapper (doctrine/mongodb-odm): library that provides a PHP object mapping functionality for MongoDB;
  • MongoDB Adapter (alcaeus/mongo-php-adapter): It provides the API of ext-mongo built on top of mongo-php-library, thus being compatible with PHP 7;
  • MongoDB driver library (mongodb/mongodb): provides a high-level abstraction around the lower-level drivers for PHP;
  • MongoDB PHP extension: low level driver extension for PHP and HHVM. Please pay attention on the MongoDB driver. Don’t use the legacy MongoDB driver (http://php.net/manual/en/book.mongo.php).

Install MongoDB PHP extension

First of all you need to enable the MongoDB extension in your PHP installation.

A good starting point is http://php.net/manual/en/mongodb.installation.php

On my machine (OS X El Capitain 10.11) I installed the mongoDB extension via Homebrew with the command:

$ brew install homebrew/php/php70-mongodb

This command will create the file:

/usr/local/etc/php/7.0/conf.d/ext-mongodb.ini

The content of the file is

[mongodb]
extension=”/usr/local/opt/php70-mongodb/mongodb.so”

Check if the extension is enabled with the command:

$ php -i | grep mongo

the output is

/usr/local/etc/php/7.0/conf.d/ext-mongodb.ini,
mongodb
mongodb support => enabled
mongodb version => 1.1.8
mongodb stability => stable
libmongoc version => 1.3.5
mongodb.debug => no value => no value

Ok, now you have your mongoDB extension enabled.

Create Symfony3 project

Create the new project with “symfony” command (sfmongodbmedium is the name of the project):

$ symfony new sfmongodbmedium
$ cd sfmongodbmedium

Make sure that you have Composer installed

To install composer you can read page: https://getcomposer.org/download/

Install the MongoDB adapter

You need to install the MongoDB adapter (for PHP7):

composer require alcaeus/mongo-php-adapter

This command will configure your composer.json file and add 2 packages: alcaeus/mongo-php-adapter and its dependency mongodb/mongodb.

With this command you have your high level libraries for accessing MongoDB API.

Install MongoDB Doctrine packages

To install MongoDB Doctrine Packages:

composer require doctrine/mongodb-odm doctrine/mongodb-odm-bundle

This command will install the Object Document Mapper for MongoDB , its Symfony Bundle and its dependency doctrine/mongodb.

Now, you have your Symfony/Doctrine/MongoDB stack installed on your project. You can go on with the configuration.

Configuration and setup

Now you need to configure your Symfony3 installation to use your Symfony/Doctrine/MongoDB stack.

You can follow the instruction here: http://symfony.com/doc/current/bundles/DoctrineMongoDBBundle/index.html

You need to add the lines:

use Doctrine\ODM\MongoDB\Mapping\Driver\AnnotationDriver;
AnnotationDriver::registerAnnotationClasses();

in your app/autoload.php file.

You need to add the lines:

new Doctrine\Bundle\MongoDBBundle\DoctrineMongoDBBundle(),

in your bundles array list in your app/AppKernel.php file.

You need to add lines:

mongodb_server: “mongodb://localhost:27017”

in your app/config/parameters.yml.

Now you are ready to code your Symfony3 + MongoDB combo!

--

--

Roberto Butti

I’m technophile. Vuejs and Laravel enthusiast! #vuejs #laravel. I love #coding