|
| 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 | +- Advanced query features: filtering, sorting, pagination |
| 15 | +- RBAC: per-table role-based access control |
| 16 | +- Admin panel (minimal) |
| 17 | +- OpenAPI (Swagger) JSON endpoint for instant docs |
| 18 | +- Clean PSR-4 codebase |
| 19 | +- PHPUnit tests and extensible architecture |
| 20 | + |
| 21 | +--- |
| 22 | + |
| 23 | +## 📦 Installation |
| 24 | + |
| 25 | +```bash |
| 26 | +composer create-project bitshost/php-crud-api-generator |
| 27 | +``` |
| 28 | + |
| 29 | +--- |
| 30 | + |
| 31 | +## ⚙️ Configuration |
| 32 | + |
| 33 | +Copy and edit config files: |
| 34 | + |
| 35 | +```bash |
| 36 | +cp config/db.example.php config/db.php |
| 37 | +cp config/api.example.php config/api.php |
| 38 | +``` |
| 39 | + |
| 40 | +Edit `config/db.php`: |
| 41 | + |
| 42 | +```php |
| 43 | +return [ |
| 44 | + 'host' => 'localhost', |
| 45 | + 'dbname' => 'your_database', |
| 46 | + 'user' => 'your_db_user', |
| 47 | + 'pass' => 'your_db_password', |
| 48 | + 'charset' => 'utf8mb4' |
| 49 | +]; |
| 50 | +``` |
| 51 | + |
| 52 | +Edit `config/api.php`: |
| 53 | + |
| 54 | +```php |
| 55 | +return [ |
| 56 | + 'auth_enabled' => false, // true to require authentication |
| 57 | + 'auth_method' => 'apikey', // 'apikey', 'basic', 'jwt', 'oauth' |
| 58 | + 'api_keys' => ['changeme123'], // API keys for 'apikey' |
| 59 | + 'basic_users' => ['admin' => 'secret'], // Users for 'basic' and 'jwt' |
| 60 | + 'jwt_secret' => 'YourSuperSecretKey', |
| 61 | + 'jwt_issuer' => 'yourdomain.com', |
| 62 | + 'jwt_audience' => 'yourdomain.com', |
| 63 | + 'oauth_providers' => [ |
| 64 | + // 'google' => ['client_id' => '', 'client_secret' => '', ...] |
| 65 | + ] |
| 66 | +]; |
| 67 | +``` |
| 68 | + |
| 69 | +--- |
| 70 | + |
| 71 | +## 🔐 Authentication Modes |
| 72 | + |
| 73 | +- **No auth:** `'auth_enabled' => false` |
| 74 | +- **API Key:** `'auth_enabled' => true, 'auth_method' => 'apikey'` |
| 75 | + Client: `X-API-Key` header or `?api_key=...` |
| 76 | +- **Basic Auth:** `'auth_method' => 'basic'` |
| 77 | + Client: HTTP Basic Auth |
| 78 | +- **JWT:** `'auth_method' => 'jwt'` |
| 79 | + 1. `POST /index.php?action=login` with `username` and `password` (from `basic_users`) |
| 80 | + 2. Use returned token as `Authorization: Bearer <token>` |
| 81 | +- **OAuth (future):** `'auth_method' => 'oauth'` |
| 82 | + (Implement provider logic as needed) |
| 83 | + |
| 84 | +--- |
| 85 | + |
| 86 | +## 📚 API Endpoints |
| 87 | + |
| 88 | +All requests go through `public/index.php` with `action` parameter. |
| 89 | + |
| 90 | +| Action | Method | Usage Example | |
| 91 | +|-----------|--------|------------------------------------------------------------| |
| 92 | +| tables | GET | `/index.php?action=tables` | |
| 93 | +| columns | GET | `/index.php?action=columns&table=users` | |
| 94 | +| list | GET | `/index.php?action=list&table=users` | |
| 95 | +| read | GET | `/index.php?action=read&table=users&id=1` | |
| 96 | +| create | POST | `/index.php?action=create&table=users` (form POST) | |
| 97 | +| update | POST | `/index.php?action=update&table=users&id=1` (form POST) | |
| 98 | +| delete | POST | `/index.php?action=delete&table=users&id=1` | |
| 99 | +| openapi | GET | `/index.php?action=openapi` | |
| 100 | +| login | POST | `/index.php?action=login` (JWT only) | |
| 101 | + |
| 102 | +--- |
| 103 | + |
| 104 | +## 🤖 Example `curl` Commands |
| 105 | + |
| 106 | +```sh |
| 107 | +curl http://localhost/index.php?action=tables |
| 108 | +curl -H "X-API-Key: changeme123" "http://localhost/index.php?action=list&table=users" |
| 109 | +curl -X POST -d "username=admin&password=secret" http://localhost/index.php?action=login |
| 110 | +curl -H "Authorization: Bearer <token>" "http://localhost/index.php?action=list&table=users" |
| 111 | +curl -u admin:secret "http://localhost/index.php?action=list&table=users" |
| 112 | +``` |
| 113 | + |
| 114 | +--- |
| 115 | + |
| 116 | + |
| 117 | +### 🔄 Advanced Query Features (Filtering, Sorting, Pagination) |
| 118 | + |
| 119 | +The `list` action endpoint now supports advanced query parameters: |
| 120 | + |
| 121 | +| Parameter | Type | Description | |
| 122 | +|--------------|---------|---------------------------------------------------------------------------------------------------| |
| 123 | +| `filter` | string | Filter rows by column values. Format: `filter=col1:value1,col2:value2`. Use `%` for wildcards. | |
| 124 | +| `sort` | string | Sort by columns. Comma-separated. Use `-` prefix for DESC. Example: `sort=-created_at,name` | |
| 125 | +| `page` | int | Page number (1-based). Default: `1` | |
| 126 | +| `page_size` | int | Number of rows per page (max 100). Default: `20` | |
| 127 | + |
| 128 | +**Examples:** |
| 129 | + |
| 130 | +- `GET /index.php?action=list&table=users&filter=name:Alice` |
| 131 | +- `GET /index.php?action=list&table=users&sort=-created_at,name` |
| 132 | +- `GET /index.php?action=list&table=users&page=2&page_size=10` |
| 133 | +- `GET /index.php?action=list&table=users&filter=email:%gmail.com&sort=name&page=1&page_size=5` |
| 134 | + |
| 135 | +**Response:** |
| 136 | +```json |
| 137 | +{ |
| 138 | + "data": [ ... array of rows ... ], |
| 139 | + "meta": { |
| 140 | + "total": 47, |
| 141 | + "page": 2, |
| 142 | + "page_size": 10, |
| 143 | + "pages": 5 |
| 144 | + } |
| 145 | +} |
| 146 | +``` |
| 147 | + |
| 148 | +--- |
| 149 | + |
| 150 | +### 📝 OpenAPI Path Example |
| 151 | + |
| 152 | +For `/index.php?action=list&table={table}`: |
| 153 | + |
| 154 | +```yaml |
| 155 | +get: |
| 156 | + summary: List rows in {table} with optional filtering, sorting, and pagination |
| 157 | + parameters: |
| 158 | + - name: table |
| 159 | + in: query |
| 160 | + required: true |
| 161 | + schema: { type: string } |
| 162 | + - name: filter |
| 163 | + in: query |
| 164 | + required: false |
| 165 | + schema: { type: string } |
| 166 | + description: | |
| 167 | + Filter rows by column values. Example: filter=name:Alice,email:%gmail.com |
| 168 | + - name: sort |
| 169 | + in: query |
| 170 | + required: false |
| 171 | + schema: { type: string } |
| 172 | + description: | |
| 173 | + Sort by columns. Example: sort=-created_at,name |
| 174 | + - name: page |
| 175 | + in: query |
| 176 | + required: false |
| 177 | + schema: { type: integer, default: 1 } |
| 178 | + description: Page number (1-based) |
| 179 | + - name: page_size |
| 180 | + in: query |
| 181 | + required: false |
| 182 | + schema: { type: integer, default: 20, maximum: 100 } |
| 183 | + description: Number of rows per page (max 100) |
| 184 | + responses: |
| 185 | + '200': |
| 186 | + description: List of rows with pagination meta |
| 187 | + content: |
| 188 | + application/json: |
| 189 | + schema: |
| 190 | + type: object |
| 191 | + properties: |
| 192 | + data: |
| 193 | + type: array |
| 194 | + items: { type: object } |
| 195 | + meta: |
| 196 | + type: object |
| 197 | + properties: |
| 198 | + total: { type: integer } |
| 199 | + page: { type: integer } |
| 200 | + page_size: { type: integer } |
| 201 | + pages: { type: integer } |
| 202 | +``` |
| 203 | +
|
| 204 | +## 🛡️ Security Notes |
| 205 | +
|
| 206 | +- **Enable authentication for any public deployment!** |
| 207 | +- Never commit real credentials—use `.gitignore` and example configs. |
| 208 | +- Restrict DB user privileges. |
| 209 | + |
| 210 | +--- |
| 211 | + |
| 212 | +## 🧪 Running Tests |
| 213 | + |
| 214 | +```bash |
| 215 | +./vendor/bin/phpunit |
| 216 | +``` |
| 217 | + |
| 218 | +--- |
| 219 | + |
| 220 | +## 🗺️ Roadmap |
| 221 | + |
| 222 | +- Relations / Linked Data (auto-join, populate, or expand related records) |
| 223 | +- API Versioning (when needed) |
| 224 | +- OAuth/SSO (if targeting SaaS/public) |
| 225 | +- More DB support (Postgres, SQLite, etc.) |
| 226 | +- Analytics & promotion endpoints |
| 227 | + |
| 228 | +--- |
| 229 | + |
| 230 | +## 📄 License |
| 231 | + |
| 232 | +MIT |
| 233 | + |
| 234 | +--- |
| 235 | + |
| 236 | +## 🙌 Credits |
| 237 | + |
| 238 | +Built by [BitHost](https://github.com/BitsHost). PRs/issues welcome! |
0 commit comments