|
1 | | -# PHP-CRUD-API-Generator |
2 | | -PHP CRUD API Generator |
| 1 | +# PHP CRUD API Generator |
| 2 | + |
| 3 | +Expose your MySQL/MariaDB database as a secure, flexible, and instant REST-like API. |
| 4 | +Features optional authentication (API key, Basic Auth, JWT, OAuth-ready), |
| 5 | +OpenAPI (Swagger) docs, and zero code generation. |
| 6 | + |
| 7 | +--- |
| 8 | + |
| 9 | +## 🚀 Features |
| 10 | + |
| 11 | +- Auto-discovers tables and columns |
| 12 | +- Full CRUD endpoints for any table |
| 13 | +- Configurable authentication (API Key, Basic Auth, JWT, or none) |
| 14 | +- OpenAPI (Swagger) JSON endpoint for instant docs |
| 15 | +- Clean PSR-4 codebase |
| 16 | +- PHPUnit tests and extensible architecture |
| 17 | + |
| 18 | +--- |
| 19 | + |
| 20 | +## 📦 Installation |
| 21 | + |
| 22 | +```bash |
| 23 | +composer create-project yourvendor/php-crud-api-generator |
| 24 | +``` |
| 25 | + |
| 26 | +--- |
| 27 | + |
| 28 | +## ⚙️ Configuration |
| 29 | + |
| 30 | +Copy and edit config files: |
| 31 | + |
| 32 | +```bash |
| 33 | +cp config/db.example.php config/db.php |
| 34 | +cp config/api.example.php config/api.php |
| 35 | +``` |
| 36 | + |
| 37 | +Edit `config/db.php`: |
| 38 | + |
| 39 | +```php |
| 40 | +return [ |
| 41 | + 'host' => 'localhost', |
| 42 | + 'dbname' => 'your_database', |
| 43 | + 'user' => 'your_db_user', |
| 44 | + 'pass' => 'your_db_password', |
| 45 | + 'charset' => 'utf8mb4' |
| 46 | +]; |
| 47 | +``` |
| 48 | + |
| 49 | +Edit `config/api.php`: |
| 50 | + |
| 51 | +```php |
| 52 | +return [ |
| 53 | + 'auth_enabled' => false, // true to require authentication |
| 54 | + 'auth_method' => 'apikey', // 'apikey', 'basic', 'jwt', 'oauth' |
| 55 | + 'api_keys' => ['changeme123'], // API keys for 'apikey' |
| 56 | + 'basic_users' => ['admin' => 'secret'], // Users for 'basic' and 'jwt' |
| 57 | + 'jwt_secret' => 'YourSuperSecretKey', |
| 58 | + 'jwt_issuer' => 'yourdomain.com', |
| 59 | + 'jwt_audience' => 'yourdomain.com', |
| 60 | + 'oauth_providers' => [ |
| 61 | + // 'google' => ['client_id' => '', 'client_secret' => '', ...] |
| 62 | + ] |
| 63 | +]; |
| 64 | +``` |
| 65 | + |
| 66 | +--- |
| 67 | + |
| 68 | +## 🔐 Authentication Modes |
| 69 | + |
| 70 | +- **No auth:** `'auth_enabled' => false` |
| 71 | +- **API Key:** `'auth_enabled' => true, 'auth_method' => 'apikey'` |
| 72 | + Client: `X-API-Key` header or `?api_key=...` |
| 73 | +- **Basic Auth:** `'auth_method' => 'basic'` |
| 74 | + Client: HTTP Basic Auth |
| 75 | +- **JWT:** `'auth_method' => 'jwt'` |
| 76 | + 1. `POST /index.php?action=login` with `username` and `password` (from `basic_users`) |
| 77 | + 2. Use returned token as `Authorization: Bearer <token>` |
| 78 | +- **OAuth (future):** `'auth_method' => 'oauth'` |
| 79 | + (Implement provider logic as needed) |
| 80 | + |
| 81 | +--- |
| 82 | + |
| 83 | +## 📚 API Endpoints |
| 84 | + |
| 85 | +All requests go through `public/index.php` with `action` parameter. |
| 86 | + |
| 87 | +| Action | Method | Usage Example | |
| 88 | +|-----------|--------|------------------------------------------------------------| |
| 89 | +| tables | GET | `/index.php?action=tables` | |
| 90 | +| columns | GET | `/index.php?action=columns&table=users` | |
| 91 | +| list | GET | `/index.php?action=list&table=users` | |
| 92 | +| read | GET | `/index.php?action=read&table=users&id=1` | |
| 93 | +| create | POST | `/index.php?action=create&table=users` (form POST) | |
| 94 | +| update | POST | `/index.php?action=update&table=users&id=1` (form POST) | |
| 95 | +| delete | POST | `/index.php?action=delete&table=users&id=1` | |
| 96 | +| openapi | GET | `/index.php?action=openapi` | |
| 97 | +| login | POST | `/index.php?action=login` (JWT only) | |
| 98 | + |
| 99 | +--- |
| 100 | + |
| 101 | +## 🤖 Example `curl` Commands |
| 102 | + |
| 103 | +```sh |
| 104 | +curl http://localhost/index.php?action=tables |
| 105 | +curl -H "X-API-Key: changeme123" "http://localhost/index.php?action=list&table=users" |
| 106 | +curl -X POST -d "username=admin&password=secret" http://localhost/index.php?action=login |
| 107 | +curl -H "Authorization: Bearer <token>" "http://localhost/index.php?action=list&table=users" |
| 108 | +curl -u admin:secret "http://localhost/index.php?action=list&table=users" |
| 109 | +``` |
| 110 | + |
| 111 | +--- |
| 112 | + |
| 113 | +## 🛡️ Security Notes |
| 114 | + |
| 115 | +- **Enable authentication for any public deployment!** |
| 116 | +- Never commit real credentials—use `.gitignore` and example configs. |
| 117 | +- Restrict DB user privileges. |
| 118 | + |
| 119 | +--- |
| 120 | + |
| 121 | +## 🧪 Running Tests |
| 122 | + |
| 123 | +```bash |
| 124 | +./vendor/bin/phpunit |
| 125 | +``` |
| 126 | + |
| 127 | +--- |
| 128 | + |
| 129 | +## 🗺️ Roadmap |
| 130 | + |
| 131 | +- RESTful route aliases (`/users/1`) |
| 132 | +- OAuth2 provider integration |
| 133 | +- More DB support (Postgres, SQLite) |
| 134 | +- Pagination, filtering, relations |
| 135 | + |
| 136 | +--- |
| 137 | + |
| 138 | +## 📄 License |
| 139 | + |
| 140 | +MIT |
| 141 | + |
| 142 | +--- |
| 143 | + |
| 144 | +## 🙌 Credits |
| 145 | + |
| 146 | +Built by [Your Name](https://github.com/yourusername). PRs/issues welcome! |
0 commit comments