|
| 1 | +# PHP REST API Pro Kit |
| 2 | + |
| 3 | +A production-ready Raw PHP REST API Starter Kit with JWT authentication, user management, file uploads, caching, rate limiting, and Docker support. |
| 4 | + |
| 5 | +[](https://php.net) |
| 6 | +[](LICENSE) |
| 7 | +[](docker-compose.yml) |
| 8 | + |
| 9 | +## 🚀 Features |
| 10 | + |
| 11 | +- ✅ **JWT Authentication** - Secure token-based authentication |
| 12 | +- ✅ **User Management** - Complete CRUD operations |
| 13 | +- ✅ **File Upload/Download** - Secure file handling |
| 14 | +- ✅ **Rate Limiting** - API request throttling |
| 15 | +- ✅ **Input Validation** - Request data validation |
| 16 | +- ✅ **Caching System** - File-based caching |
| 17 | +- ✅ **Error Handling** - Centralized error management |
| 18 | +- ✅ **Logging** - Request/error logging |
| 19 | +- ✅ **Health Checks** - System monitoring |
| 20 | +- ✅ **Docker Support** - Containerized deployment |
| 21 | +- ✅ **PHPUnit Testing** - Comprehensive test suite |
| 22 | +- ✅ **API Documentation** - Complete endpoint docs |
| 23 | + |
| 24 | +## 📁 Project Structure |
| 25 | + |
| 26 | +``` |
| 27 | +├── app/ |
| 28 | +│ ├── config/ # Configuration files |
| 29 | +│ ├── controllers/ # Request handlers |
| 30 | +│ ├── core/ # Core framework classes |
| 31 | +│ ├── database/ # Migrations and seeders |
| 32 | +│ ├── exceptions/ # Exception handlers |
| 33 | +│ ├── helpers/ # Utility classes |
| 34 | +│ ├── middleware/ # Request middleware |
| 35 | +│ ├── models/ # Data models |
| 36 | +│ ├── routes/ # Route definitions |
| 37 | +│ ├── services/ # Business logic |
| 38 | +│ └── tests/ # Test files |
| 39 | +├── bootstrap/ # Application bootstrap |
| 40 | +├── docs/ # API documentation |
| 41 | +├── public/ # Web server document root |
| 42 | +├── storage/ # Logs, cache, uploads |
| 43 | +└── vendor/ # Composer dependencies |
| 44 | +``` |
| 45 | + |
| 46 | +## ⚡ Quick Start |
| 47 | + |
| 48 | +### 1. Clone Repository |
| 49 | +```bash |
| 50 | +git clone https://github.com/jmrashed/php-rest-api-pro-kit.git |
| 51 | +cd php-rest-api-pro-kit |
| 52 | +composer install |
| 53 | +``` |
| 54 | + |
| 55 | +### 2. Environment Setup |
| 56 | +```bash |
| 57 | +cp .env.example .env |
| 58 | +# Edit .env with your database credentials |
| 59 | +``` |
| 60 | + |
| 61 | +### 3. Database Setup |
| 62 | +```bash |
| 63 | +# Import the database schema |
| 64 | +mysql -u root -p < app/database/Database.sql |
| 65 | +``` |
| 66 | + |
| 67 | +### 4. Start Development Server |
| 68 | +```bash |
| 69 | +# PHP Built-in Server |
| 70 | +php -S localhost:8000 -t public |
| 71 | + |
| 72 | +# Or with Docker |
| 73 | +docker-compose up -d |
| 74 | +``` |
| 75 | + |
| 76 | +### 5. Test the API |
| 77 | +```bash |
| 78 | +curl http://localhost:8000/api/health |
| 79 | +``` |
| 80 | + |
| 81 | +## 📚 API Endpoints |
| 82 | + |
| 83 | +### Authentication |
| 84 | +- `POST /api/auth/register` - Register new user |
| 85 | +- `POST /api/auth/login` - Login user |
| 86 | +- `POST /api/auth/logout` - Logout user |
| 87 | + |
| 88 | +### Users (Protected) |
| 89 | +- `GET /api/users` - Get all users (paginated) |
| 90 | +- `GET /api/users/{id}` - Get user by ID |
| 91 | +- `POST /api/users` - Create user |
| 92 | +- `PUT /api/users/{id}` - Update user |
| 93 | +- `DELETE /api/users/{id}` - Delete user |
| 94 | + |
| 95 | +### Files (Protected) |
| 96 | +- `POST /api/files/upload` - Upload file |
| 97 | +- `DELETE /api/files/{id}` - Delete file |
| 98 | + |
| 99 | +### System |
| 100 | +- `GET /api/health` - Health check |
| 101 | +- `GET /api/health/info` - System info |
| 102 | + |
| 103 | +### Example Usage |
| 104 | +```bash |
| 105 | +# Register |
| 106 | +curl -X POST http://localhost:8000/api/auth/register \ |
| 107 | + -H "Content-Type: application/json" \ |
| 108 | + -d '{"name":"John Doe","email":"john@example.com","password":"password123"}' |
| 109 | + |
| 110 | +# Login |
| 111 | +curl -X POST http://localhost:8000/api/auth/login \ |
| 112 | + -H "Content-Type: application/json" \ |
| 113 | + -d '{"email":"john@example.com","password":"password123"}' |
| 114 | + |
| 115 | +# Get users (with token) |
| 116 | +curl -X GET http://localhost:8000/api/users \ |
| 117 | + -H "Authorization: Bearer YOUR_JWT_TOKEN" |
| 118 | +``` |
| 119 | + |
| 120 | +## 🏗️ Architecture |
| 121 | + |
| 122 | +### Core Components |
| 123 | +- **Application**: Main app bootstrap and container |
| 124 | +- **Router**: URL routing and middleware pipeline |
| 125 | +- **Request/Response**: HTTP abstraction layer |
| 126 | +- **Database**: PDO wrapper with query builder |
| 127 | +- **Authentication**: JWT-based auth system |
| 128 | +- **Cache**: File-based caching system |
| 129 | +- **Validation**: Input validation and sanitization |
| 130 | + |
| 131 | +### Security Features |
| 132 | +- Password hashing (bcrypt) |
| 133 | +- JWT token authentication |
| 134 | +- Rate limiting (60 requests/hour per IP) |
| 135 | +- Input sanitization and validation |
| 136 | +- CORS middleware |
| 137 | +- SQL injection protection (prepared statements) |
| 138 | + |
| 139 | +## 🧪 Testing |
| 140 | + |
| 141 | +```bash |
| 142 | +# Run all tests |
| 143 | +vendor/bin/phpunit |
| 144 | + |
| 145 | +# Run specific test |
| 146 | +vendor/bin/phpunit app/tests/Unit/UserTest.php |
| 147 | +``` |
| 148 | + |
| 149 | +## 🐳 Docker Deployment |
| 150 | + |
| 151 | +```bash |
| 152 | +# Build and run |
| 153 | +docker-compose up -d |
| 154 | + |
| 155 | +# View logs |
| 156 | +docker-compose logs -f app |
| 157 | + |
| 158 | +# Stop services |
| 159 | +docker-compose down |
| 160 | +``` |
| 161 | + |
| 162 | +## 🛠️ Requirements |
| 163 | + |
| 164 | +- PHP 8.0+ |
| 165 | +- MySQL 5.7+ |
| 166 | +- Composer |
| 167 | +- Docker (optional) |
| 168 | + |
| 169 | +## 🤝 Contributing |
| 170 | + |
| 171 | +1. Fork the repository |
| 172 | +2. Create feature branch (`git checkout -b feature/amazing-feature`) |
| 173 | +3. Commit changes (`git commit -m 'Add amazing feature'`) |
| 174 | +4. Push to branch (`git push origin feature/amazing-feature`) |
| 175 | +5. Open Pull Request |
| 176 | + |
| 177 | +## 📄 License |
| 178 | + |
| 179 | +This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details. |
| 180 | + |
| 181 | +## 👨💻 Author |
| 182 | + |
| 183 | +**Md Rashedul Islam** |
| 184 | +- GitHub: [@jmrashed](https://github.com/jmrashed) |
| 185 | +- LinkedIn: [Md Rashedul Islam](https://linkedin.com/in/jmrashed) |
| 186 | + |
| 187 | +## ⭐ Show your support |
| 188 | + |
| 189 | +Give a ⭐️ if this project helped you! |
0 commit comments