This repository contains a ToDo API built with Symfony 6. The API allows users to manage tasks, including creating, retrieving, updating, and deleting tasks.
- Requirements
- Installation
- Configuration
- Database Setup
- Running the Application
- API Endpoints
- Running Tests
- Contributing
- License
Before setting up the project, ensure your environment meets the following requirements:
- PHP: Version 8.1 or higher
- Composer: Version 2.0 or higher
- Symfony CLI: Optional but recommended for local development
- PostgreSQL: Version 16 or higher (if not using Docker)
- Docker: Version 20.10 or higher (if using Docker)
- Docker Compose: Version 1.28 or higher (if using Docker)
Ensure the following PHP extensions are enabled:
pdo_pgsql
(for PostgreSQL database connection)intl
(for internationalization support)mbstring
(for string handling)openssl
(for security and encryption)json
(for JSON data handling)xml
(for XML parsing)
To get started with this project, you need to have PHP, Composer, and Symfony CLI installed on your machine.
git clone https://github.com/pusachev/symfony-6-todo-api.git
cd symfony-6-todo-api
Run the following command to install the PHP dependencies:
composer install
Copy the .env file to .env.local and configure the database URL and other environment variables as needed:
cp .env .env.local
Update the DATABASE_URL in the .env.local file:
DATABASE_URL="postgresql://db_user:db_password@127.0.0.1:5432/db_name?serverVersion=16&charset=utf8"
Generate secret key pair for JWT
php bin/console lexik:jwt:generate-keypair
php bin/console doctrine:database:create
php bin/console doctrine:migrations:migrate
If you need to truncate all tables in the database (use with caution):
php bin/console app:database:truncate
You can start the Symfony development server by running:
symfony server:start
docker-compose up -d
The application will be accessible at http://127.0.0.1:8000.
Here are the main API endpoints available in this ToDo application:
- Register a User: POST /api/register
- Login User: POST /api/login
- Create a Task: POST /api/tasks
- Get All Tasks: GET /api/tasks
- Get a Single Task: GET /api/tasks/{id}
- Update a Task: PUT /api/tasks/{id}
- Delete a Task: DELETE /api/tasks/{id}
Register
curl -X POST http://127.0.0.1:8000/api/register -H "Content-Type: application/json" -d '{"email": "user@example.com", "password": "password"}'
Login
curl -X POST http://127.0.0.1:8000/api/login -H "Content-Type: application/json" -d '{"email": "user@example.com", "password": "password"}'
Create a Task
curl -X POST http://localhost:8000/api/tasks -H "Content-Type: application/json" -H "Authorization: Bearer {JWT_token}" -d '{"title": "New Task", "description": "Task Description"}'
Get All User Tasks
curl -X GET http://localhost:8000/api/tasks -H "Content-Type: application/json" -H "Authorization: Bearer {JWT_token}"
Get a Single Task
curl -X GET http://localhost:8000/api/tasks/{task_id} -H "Content-Type: application/json" -H "Authorization: Bearer {JWT_token}"
Update a Task
curl -X PUT http://localhost:8000/api/tasks/{task_id} -H "Content-Type: application/json" -H "Authorization: Bearer {JWT_token}" -d '{"title": "Updated Title", "description": "Updated Description"}'
Delete a Task
curl -X DELETE http://localhost:8000/api/tasks/{task_id} -H "Content-Type: application/json" -H "Authorization: Bearer {JWT_token}"
This project includes unit and functional tests.
Before running the tests, create a test database:
php bin/console doctrine:database:create --env=test
php bin/console doctrine:schema:create --env=test
Run the tests using PHPUnit:
php bin/phpunit
The tests are configured to run with database isolation to ensure a clean state for each test.
Contributions are welcome! Please feel free to submit a Pull Request.
This project is open-source and available under the MIT License.