This is a base application for a REST API in the Symfony framework that I created to speed up backend development.
The base contains essential utilities and functions for validation, logging, error handling, and the user system.
The application is set up with the Nelmio API Doc Bundle for testing endpoints in the browser at /api/doc
and in JSON format at /api/doc.json
.
All API requests must have static X-API-Token header set, which is used for validating the request. The token is set in the .env file.
Error handling is managed by the handleError
function in the ErrorManager class
, which triggers an exception that is listened to by the ExceptionEventSubscriber
. The subscriber logs the exception into the exception log and displays an error response for the user.
For logging, there is the LogManager class
, which contains functions for saving and reading logs from the database through the Log entity.
The user system is managed by the UserManager class
, and login works using a JWT token in the authorization header, thanks to Symfony Security and Lexik JWT.
The application has CLI commands for the LogManager and UserManager, and overall system management through the CLI.
All requests accept input data in JSON format and return JSON data back to the client.
curl -X POST http://localhost/api/auth/register \
-H "Content-Type: application/json" \
-H "X-API-TOKEN: 1234" \
-d '{
"email": "[email protected]",
"first-name": "John",
"last-name": "Doe",
"password": "securePassword123"
}'
curl -X POST http://localhost/api/auth/login \
-H "Content-Type: application/json" \
-H "X-API-TOKEN: 1234" \
-d '{
"email": "[email protected]",
"password": "test"
}'
curl -X POST http://localhost/api/auth/logout \
-H "Content-Type: application/json" \
-H "X-API-TOKEN: 1234" \
-H "Authorization: Bearer <token>"
curl -X GET http://localhost/api/user/info \
-H "Content-Type: application/json" \
-H "X-API-TOKEN: 1234" \
-H "Authorization: Bearer <token>"
curl -X PATCH http://localhost/api/user/data/update/password \
-H "Content-Type: application/json" \
-H "X-API-TOKEN: 1234" \
-H "Authorization: Bearer <token>" \
-d '{
"new-password": "asdfghjkoiuzrewq"
}'
curl -X PATCH http://localhost/api/user/data/update/role \
-H "Content-Type: application/json" \
-H "X-API-TOKEN: 1234" \
-H "Authorization: Bearer <token>" \
-d '{
"user-id": 1,
"task": "add",
"role": "ROLE_TEST"
}'
curl -X PATCH http://localhost/api/user/data/update/status \
-H "Content-Type: application/json" \
-H "X-API-TOKEN: 1234" \
-H "Authorization: Bearer <token>" \
-d '{
"user-id": 2,
"status": "idk"
}'
curl -X PATCH http://localhost/api/user/delete \
-H "Content-Type: application/json" \
-H "X-API-TOKEN: 1234" \
-H "Authorization: Bearer <token>" \
-d '{
"user-id": 3
}'
curl -X GET http://localhost/api/user/list \
-H "Content-Type: application/json" \
-H "X-API-TOKEN: 1234" \
-H "Authorization: Bearer <token>"
This software is licensed under the MIT license.