This is a simple REST API written in PHP for instrucional purposes only. It is written from scratch without using any framework, uses MySQL as database and PDO for database operations. It is developed following an MVC architecture.
- Clone the repository
- To run the API, you need to have PHP and MySQL installed on your system. You can use XAMPP or WAMP. I used Laragon while developing this API.
- Check if the Apache configuration file points to the public folder. If not, then change it to point to the public folder.
- Set the database credentials in the .env file, that you can clone from .env.example file.
- Clone the .hataccess file from .hataccess.example file.
- If your URL is
http://localhost/php-api
, then theRewriteBase
should be/php-api/
.
- If your URL is
- Import the database file in the database folder.
{
"success": "true/false",
"status": "200/400/401/404/500",
"message": "Message",
"error": "Error message if any",
"data": {
"object": {
"key": "value"
}
}
}
GET /users
{
"success": true,
"status": 200,
"message": "Records found",
"data": {
"users": [
{
"id": 1,
"fullname": "Johnny Doe",
"username": "johndoe",
"email": "john@email.com",
"address": "123 King St"
},
{
"id": 2,
"fullname": "Jane Doe",
"username": "janedoe",
"email": "jane@email.com",
"address": "124 Queen St"
}
]
}
}
GET /users/{id}
{
"success": true,
"status": 200,
"message": "Record found",
"data": {
"user": {
"id": 1,
"fullname": "John Doe",
"username": "johndoe",
"email": "john@email.com",
"address": "123 King St"
}
}
}
POST /users/create
{
"fullname": "Jack Doe",
"username": "jackdoe",
"email": "jack@email.com",
"password": "password",
"address": "125 King St"
}
{
"success": true,
"status": 200,
"message": "Record created",
"data": {
"user": {
"id": 3,
"fullname": "Jack Doe",
"username": "jackdoe",
"email": "jack@email.com",
"address": "125 King St"
}
}
}
PUT /users/{id}
{
"fullname": "Jack Doe",
"username": "jackdoe",
"email": "jack@email.com",
"address": "126 King St"
}
{
"success": true,
"status": 200,
"message": "Record updated",
"data": {
"user": {
"id": 3,
"fullname": "Jack Doe",
"username": "jackdoe",
"email": "jack@email.com",
"address": "126 King St"
}
}
}
DELETE /users/{id}
No JSON is returned in the response body. The status code is 204.