This repository demonstrates how to build robust, scalable REST APIs using API Platform with MongoDB as the database. The project is built using Symfony and integrates seamlessly with MongoDB Atlas for efficient CRUD operations.
- Use of API Platform to simplify API creation, documentation, and validation.
- Integration with MongoDB Atlas, a cloud-based NoSQL database.
- Complete CRUD (Create, Read, Update, Delete) operations on a MongoDB collection.
- Advanced features like data validation, filtering, and custom constraints.
- OpenAPI-compliant documentation via Swagger UI.
Ensure the following tools are installed:
- Docker
- A free MongoDB Atlas cluster. Sign up here.
git clone <URL-to-your-repository>
cd <repository-name>
Update the Dockerfile to include MongoDB extensions:
RUN apt-get update && apt-get install --no-install-recommends -y \
libcurl4-openssl-dev \
libssl-dev \
&& pecl install mongodb \
&& docker-php-ext-enable mongodb
Build the Docker container:
docker compose build --no-cache
- Create a MongoDB Atlas cluster.
- Get your connection string from Atlas (e.g.,
mongodb+srv://<user>:<password>@cluster.mongodb.net/?retryWrites=true&w=majority
). - Update the
.env
file in the project root:
MONGODB_URL=<your-atlas-uri>
MONGODB_DB=<your-database-name>
Start the containers and install required bundles:
docker compose up --wait
docker compose exec php composer require doctrine/mongodb-odm-bundle api-platform/doctrine-odm
This project includes a sample Restaurant
collection with fields like name, address, borough, and cuisine.
After the application is running, access the Swagger UI documentation:
https://localhost:8443/docs
- Create: POST
/restaurants
- Read All: GET
/restaurants
- Read One: GET
/restaurants/{id}
- Update: PATCH
/restaurants/{id}
- Delete: DELETE
/restaurants/{id}
{
"name": "The Gourmet Spot",
"address": {
"building": "123",
"street": "Elm Street",
"zipcode": "12345"
},
"borough": "Manhattan",
"cuisine": "Italian"
}
{
"name": "The Gourmet Spot",
"address": {
"building": "123",
"street": "Elm Street",
"zipcode": "12345"
},
"borough": "New York",
"cuisine": "Spanish"
}
- Field Validations: Ensure required fields like
name
,borough
, andcuisine
are present. - Custom Constraints: Includes validation for zip codes using Symfony validators.
Feel free to fork this repository and submit pull requests with enhancements or bug fixes.