API Documentation
Making an API route
API routes shoudl be declared in the `/backend/api/{your_app_name}` directory. The use of subdirectories within `/backend/api/{your_app_name}` is project dependant.
All API routes should follow the following rules:
- Each route should have its own PHP file.
- Each route PHP file should be named as such `{HTTP METHOD}-{relevant entity}.php`. Eg. `GET-person.php`.
- Each route MUST: Check the correct HTTP method, validate any sent data, return response (often from database).
Checking request method
You must first import the PHP file `/backend/helpers/helpers.php`.
// Check HTTP method
http_method_must_be("POST"); // Fails if method is not POST (Works with GET also)
Validating request data
You must first import the PHP file `/backend/helpers/helpers.php`.
// Validate incoming data
validate_request_data($_POST, "name|string", "age|number"); // Fails of either name or age has not been provided (Works with $_GET also)
Responding to a request
You must first import the PHP file `/backend/helpers/helpers.php`.
// Responds and exits
send_response(500, "Something went wrong server-side");
Verifying authentication
You must first import the PHP file `/backend/helpers/helpers.php`.
// Exists with a 403 code if the user is not logged in
must_be_authenticated();
Checking user permissions
Please see Permissions
Working with sessions
You must first import the PHP file `/backend/helpers/helpers.php`.
You cannot just call `session_start()` since this could cause issues if one of the helper functions has already called that method.
// Exists with a 403 code if the user is not logged in
safely_start_session();