"You have requested a non-existent service 'doctrine'" - Symfony 3, MongoDB & FOSUserBundle: A Troubleshooting Guide
If you're using Symfony 3 with MongoDB and FOSUserBundle and encountering the error "You have requested a non-existent service 'doctrine'", you're not alone. This frustrating issue arises because FOSUserBundle expects to work with Doctrine ORM by default, which is not the case when using MongoDB. This article will guide you through understanding the error and provide a solution to integrate FOSUserBundle seamlessly with your MongoDB-powered Symfony 3 application.
Understanding the Problem
The error message "You have requested a non-existent service 'doctrine'" signals that FOSUserBundle is looking for the Doctrine ORM service within your application. However, since you're using MongoDB, you haven't configured Doctrine ORM, leading to this conflict.
Scenario & Original Code
Let's assume you have a Symfony 3 project using MongoDB as your primary database. You've successfully integrated FOSUserBundle but are encountering the aforementioned error when trying to use its features. Here's a snippet of the relevant configuration in your config.yml
file:
# app/config/config.yml
imports:
- { resource: parameters.yml }
- { resource: security.yml }
- { resource: services.yml }
# ...
doctrine:
# ... MongoDB configuration ...
fos_user:
# ... FOSUserBundle configuration ...
The Solution: Bridging the Gap
To resolve the conflict, we need to bridge the gap between FOSUserBundle's expectations and your MongoDB-based setup. This involves replacing the dependency on Doctrine ORM with a suitable alternative for MongoDB. The most common solution is to utilize the Doctrine MongoDB ODM.
Step 1: Install Doctrine MongoDB ODM
Use Composer to install the Doctrine MongoDB ODM bundle:
composer require doctrine/mongodb-odm
Step 2: Configure Doctrine MongoDB ODM
Add the following configuration to your config.yml
file:
# app/config/config.yml
# ...
doctrine_mongodb:
connections:
default:
server: '%env(DATABASE_URL)%'
options:
connectTimeoutMS: 20000
timeoutMS: 20000
readPreference:
name: 'primaryPreferred'
# ... Add your MongoDB connection details here
document_managers:
default:
connection: default
mappings:
# ... Define mapping for your MongoDB documents ...
Step 3: Integrate FOSUserBundle with Doctrine MongoDB ODM
Replace the default Doctrine ORM configuration in FOSUserBundle with Doctrine MongoDB ODM. This step depends on your specific needs and the version of FOSUserBundle you're using. Refer to the official FOSUserBundle documentation for detailed instructions on configuring the user provider and other components.
**Here's an example using FOSUserBundle version 2.0+: **
# app/config/config.yml
# ...
fos_user:
db_driver: mongodb_odm
# ... Your other FOSUserBundle configurations ...
Note: You might need to adjust the above configurations depending on your specific project settings and FOSUserBundle version.
Final Steps:
- Clear cache: Run
php bin/console cache:clear
after making changes to your configurations. - Test: Verify that FOSUserBundle is working correctly with your MongoDB database.
Additional Insights:
- The
doctrine
service is associated with Doctrine ORM, which is not directly compatible with MongoDB. - By using the
doctrine_mongodb
service, you effectively switch FOSUserBundle's dependency to the Doctrine MongoDB ODM. - The error "You have requested a non-existent service 'doctrine'" indicates that FOSUserBundle is still looking for the Doctrine ORM service, which is why it fails.
- Understanding the difference between Doctrine ORM and Doctrine MongoDB ODM is crucial for configuring FOSUserBundle correctly.
By following these steps, you can successfully integrate FOSUserBundle with your MongoDB-powered Symfony 3 application, eliminating the "You have requested a non-existent service 'doctrine'" error. Remember to always refer to the official documentation for detailed guidance and to ensure you're using the latest best practices.