Basic API project based on Symfony 5 using: doctrine (for connection), authentication JWT token (lexik/jwt-authentication-bundle), phpunit tests, dto + validation, uuid (ramsey/uuid-doctrine), CORS controll (nelmio/cors-bundle)
- git clone project from https://github.com/Glib79/sf5_api
- rename
.env.examplefile to.env - edit
.envand setup database credentials (MYSQL_ROOT_PASSWORD, MYSQL_DATABASE, MYSQL_USER, MYSQL_PASSWORD), it's for database which will be created in docker container. - rename
app/.env.exampletoapp/.env - copy
app/.envtoapp/.env.local - edit
app/.env.localand update line:DATABASE_URL=mysql://db_user:db_password@mysql:3306/db_name?serverVersion=8- replacedb_user,db_passwordanddb_namewith your credentials (should be the same you set up in.envfile earlier).
- docker-compose build
- docker-compose up -d
- docker-compose exec php composer install
- docker-compose exec php bin/console doctrine:migrations:migrate
Migration create database structure.
Application needs ssh keys:
Create directory for ssh keys:
- mkdir app/config/jwt
Generate ssh keys:
- docker-compose exec php openssl genrsa -out config/jwt/private.pem -aes256 4096
- docker-compose exec php openssl rsa -pubout -in config/jwt/private.pem -out config/jwt/public.pem
Do not forget to put passphrase you typed during ssh key generation to app/.env.local JWT_PASSPHRASE like:
JWT_PASSPHRASE=your_passphrase
To run unit tests use command:
- docker-compose exec php bin/phpunit
Register: POST /auth/register with data:
{
"email": "test@test.com",
"password": "test123"
}
Login: POST /auth/login_check with data:
{
"username": "test@test.com",
"password": "test123"
}
You should receive Bearer token - use this token to authenticate yourself in further requests.
Add Category: POST /api/category with data:
{
"name": "Category Name"
}
Update category: PUT /api/category/{id} (replace {id} with id category to update) with data:
{
"name": "New Category Name"
}
Categories list: GET /api/categories
Single category: GET /api/category/{id} (replace {id} with id category to show)
Delete category: DELETE /api/category/{id} (replace {id} with id category to delete)
Example curl request (replace: localhost in --url http://localhost/api/category with your domain and your_token in --header 'authorization: Bearer your_token' with token received from /auth/login_check):
curl --request POST \
--url http://localhost/api/category \
--header 'authorization: Bearer your_token' \
--header 'content-type: application/json' \
--data '{
"name": "Category name"
}'