Skip to main content

๐Ÿ Quick Start

Lets start building our application using Symfony and MongoDB Doctrine ODM.

Build a skeleton projectโ€‹

We will start building a project called rentals.

composer create-project symfony/skeleton rentals

Install MongoDB Extensionโ€‹

With MacOS, ensure that OpenSSL is installed on your system. You can install it using Homebrew with the following command:

brew install openssl

Next, install pkgconf using Homebrew, as mentioned in the Homebrew documentation:

brew install pkgconf

Once installed, you can start with mongodb driver installation for PHP

cd rentals
pecl install mongodb

Verify the php.ini file to point to the installed mongodb.so.

Install driversโ€‹

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

Manually enable the bundle by adding the following line in the config/bundles.php file of your project, as mentioned in the documentation.

Create a src/Document directory for the entities defined in the next sections.

Configure the MongoDB ODMโ€‹

Add and edit the file config/packages/doctrine_mongodb.yaml and add the following content:

# config/packages/doctrine_mongodb.yaml
doctrine_mongodb:
auto_generate_proxy_classes: true
auto_generate_hydrator_classes: true
connections:
default:
server: '%env(resolve:MONGODB_URL)%'
options: {}
default_database: '%env(resolve:MONGODB_DB)%'
document_managers:
default:
auto_mapping: true
mappings:
App:
dir: '%kernel.project_dir%/src/Document'
mapping: true
type: attribute
prefix: 'App\Document'
is_bundle: false
alias: App


Install other dependenciesโ€‹

Formโ€‹

composer require symfony/form

twig bundleโ€‹

composer require symfony/twig-bundle

Assetsโ€‹

composer require symfony/asset

By the end of this page you will haveโ€‹

A project structure like this:

.
โ”œโ”€โ”€ composer.json
โ”œโ”€โ”€ composer.lock
โ”œโ”€โ”€ symfony.lock
โ”œโ”€โ”€ src
โ”‚ย ย  โ”œโ”€โ”€ Controller
โ”‚ย ย  โ”œโ”€โ”€ Document
โ”œโ”€โ”€ config
โ”‚ย ย  โ”œโ”€โ”€ packages
โ”‚ย ย  โ”‚ย ย  โ”œโ”€โ”€ doctrine_mongodb.yaml
โ”‚ย ย  โ”‚ย ย  โ”œโ”€โ”€ framework.yaml
โ”‚ย ย  โ”‚ย ย  โ”œโ”€โ”€ twig.yaml
โ”‚ย ย  โ”‚ย ย  โ””โ”€โ”€ routing.yaml
| โ”œโ”€โ”€ bundles.php
โ”œโ”€โ”€ public
โ”‚ย ย  โ”œโ”€โ”€ index.php

The necessary dependencies to start building a web application using Symfony and MongoDB ODM are now installed.