|
1 | | -# WARNING! |
2 | | -# THIS PROJECT IS UNDER CONSTRUCTION! |
3 | | - |
| 1 | +# Basic RESTful API in PHP |
| 2 | + |
| 3 | +## Overview |
| 4 | +This project is a simple RESTful API built in native PHP. It serves as a learning resource for those interested in understanding how to create a basic RESTful API from scratch. The API provides endpoints to manage user data, allowing users to perform CRUD (Create, Read, Update, Delete) operations. |
| 5 | + |
| 6 | +## Features |
| 7 | + |
| 8 | +- Fully implemented in native PHP with minimal dependencies. |
| 9 | +- Basic CRUD operations for user data. |
| 10 | +- Clean and organized code structure. |
| 11 | +- Custom error handling and responses. |
| 12 | + |
| 13 | +## API Endpoints |
| 14 | +Here are the available API endpoints: |
| 15 | +- Get All Users |
| 16 | + ```bash |
| 17 | + GET /api/users |
| 18 | + ``` |
| 19 | + Retrieves a list of all users |
| 20 | + |
| 21 | +- Add New User |
| 22 | + ```bash |
| 23 | + POST /api/users |
| 24 | + ``` |
| 25 | + Adds a new user to the database. Requires name, age, and job in the request body. |
| 26 | + |
| 27 | +- Get User by ID |
| 28 | + ```bash |
| 29 | + GET /api/users/:id |
| 30 | + ``` |
| 31 | + Retrieves user data for a specified user ID. |
| 32 | + |
| 33 | +- Update User Data |
| 34 | + ```bash |
| 35 | + PATCH /api/users/:id |
| 36 | + ``` |
| 37 | + Updates user data for a specified user ID. Requires at least one of the following fields in the request body: **name**, **age**, or **job**. |
| 38 | + |
| 39 | +- Delete User by ID |
| 40 | + ```bash |
| 41 | + DELETE /api/users/:id |
| 42 | + ``` |
| 43 | + Deletes the user with the specified user ID. |
| 44 | + |
| 45 | +## Project Structure |
| 46 | + |
| 47 | +The main components of the project include: |
| 48 | +- **public**: Contains `index.php` file as entry point for the project |
| 49 | +- **config**: Contains configuration files and environment variables. |
| 50 | +- **src/routes**: Contains route for HTTP request |
| 51 | +- **src/controllers**: Contains logic for handling API requests. Example: `UsersController.php` |
| 52 | +- **src/models**: Contains database interaction logic. Example: `User.php` |
| 53 | + |
| 54 | +## Getting Started |
| 55 | + |
| 56 | +To get started with this project, follow the steps below. |
| 57 | + |
| 58 | +### Prerequisites |
| 59 | +- PHP 7.4 or higher |
| 60 | +- A web server (e.g., Apache, Nginx, or PHP built in web server) |
| 61 | +- Composer (for dependency management) |
| 62 | + |
| 63 | +### Installation |
| 64 | +1. Clone the repository: |
| 65 | + ``` |
| 66 | + git clone https://github.com/FarrelAD/Basic-PHP-RESTful-API.git |
| 67 | + ``` |
| 68 | +2. Navigate to the project directory: |
| 69 | + ```bash |
| 70 | + cd Basic-PHP-RESTful-API |
| 71 | + ``` |
| 72 | +3. Install the dependencies: |
| 73 | + ```bash |
| 74 | + composer install |
| 75 | + ``` |
| 76 | + |
| 77 | +### Configuration |
| 78 | + |
| 79 | +Create a **.env** file in the project root directory to store your environment variables. This is essential for configuring database connections and other settings. |
| 80 | + |
| 81 | +Example .env file: |
| 82 | +```env |
| 83 | +DB_HOST=localhost |
| 84 | +DB_USER=root |
| 85 | +DB_PASS=my_password |
| 86 | +DB_NAME=db_php_restful_api |
| 87 | +``` |
| 88 | + |
| 89 | +## Usage |
| 90 | + |
| 91 | +After setting up the project, you can access the API endpoints using a tool like Postman or cURL. Below are some examples of how to use the endpoints: |
| 92 | + |
| 93 | +### Get All Users |
| 94 | +```bash |
| 95 | +curl -X GET http://localhost:8000/api/users |
| 96 | +``` |
| 97 | + |
| 98 | +### Add New User |
| 99 | +```bash |
| 100 | +curl -X POST http://localhost:8000/api/users -d "name=John Doe&age=30&job=Developer" |
| 101 | +``` |
| 102 | + |
| 103 | +### Get User By ID |
| 104 | +```bash |
| 105 | +curl -X GET http://localhost:8000/api/users/1 |
| 106 | +
|
| 107 | +``` |
| 108 | + |
| 109 | +### Update User Data |
| 110 | +```bash |
| 111 | +curl -X PATCH http://localhost:8000/api/users/1 -d '{"name": "Jane Doe"}' |
| 112 | +``` |
| 113 | + |
| 114 | +### Delete User By ID |
| 115 | +```bash |
| 116 | +curl -X DELETE http://localhost:8000/api/users/1 |
| 117 | +``` |
| 118 | + |
| 119 | +## Error Handling |
| 120 | +The API includes custom error handling to manage different scenarios, such as: |
| 121 | + |
| 122 | +- 404 Not Found: When a user is not found. |
| 123 | +- 400 Bad Request: When the request body is empty or required data is missing. |
| 124 | +- 500 Internal Server Error: For unexpected errors during processing. |
| 125 | + |
| 126 | +All responses are returned in JSON format, providing a consistent structure for success and error messages. |
| 127 | + |
| 128 | +## License |
| 129 | +This project is open-source and available under the MIT License. Feel free to modify and use it as a learning resource. |
0 commit comments