@@ -22,14 +22,18 @@ A production-ready Raw PHP REST API Starter Kit with JWT authentication, user ma
2222- ✅ ** API Documentation** - Complete endpoint docs
2323- ✅ ** Debug Bar** - Development debugging toolbar with performance monitoring
2424- ✅ ** CLI Support** - Command-line interface for development tasks
25+ - ✅ ** API Versioning** - Multiple API versions with backward compatibility
2526
2627## 📁 Project Structure
2728
2829```
2930├── app/
31+ │ ├── api/ # API versioning system
3032│ ├── cli/ # CLI commands and console
3133│ ├── config/ # Configuration files
3234│ ├── controllers/ # Request handlers
35+ │ │ ├── v1/ # Version 1 controllers
36+ │ │ └── v2/ # Version 2 controllers
3337│ ├── core/ # Core framework classes
3438│ ├── database/ # Migrations and seeders
3539│ ├── debugbar/ # Debug bar system
@@ -38,6 +42,10 @@ A production-ready Raw PHP REST API Starter Kit with JWT authentication, user ma
3842│ ├── middleware/ # Request middleware
3943│ ├── models/ # Data models
4044│ ├── routes/ # Route definitions
45+ │ │ ├── api.php # Legacy API routes (backward compatibility)
46+ │ │ ├── api_v1.php # Version 1 API routes
47+ │ │ ├── api_v2.php # Version 2 API routes
48+ │ │ └── web.php # Web routes
4149│ ├── services/ # Business logic
4250│ └── tests/ # Test files
4351├── console # CLI entry point
@@ -130,41 +138,64 @@ Password: admin123
130138
131139## 📚 API Endpoints
132140
133- ### Authentication
141+ ### Versioned Endpoints
142+
143+ #### V1 API (Standard Format)
144+ - ` GET /api/v1/health ` - Health check
145+ - ` POST /api/v1/auth/register ` - Register new user
146+ - ` POST /api/v1/auth/login ` - Login user
147+ - ` GET /api/v1/users ` - Get all users (paginated)
148+ - ` GET /api/v1/users/{id} ` - Get user by ID
149+ - ` POST /api/v1/users ` - Create user
150+ - ` PUT /api/v1/users/{id} ` - Update user
151+ - ` DELETE /api/v1/users/{id} ` - Delete user
152+
153+ #### V2 API (Enhanced Format)
154+ - ` GET /api/v2/health ` - Health check with metadata
155+ - ` POST /api/v2/auth/register ` - Register with enhanced response
156+ - ` POST /api/v2/auth/login ` - Login with structured response
157+ - ` GET /api/v2/users ` - Get users with enhanced pagination
158+ - ` GET /api/v2/users/{id} ` - Get user with metadata
159+ - ` POST /api/v2/users ` - Create user with structured response
160+ - ` PUT /api/v2/users/{id} ` - Update user with action tracking
161+ - ` DELETE /api/v2/users/{id} ` - Delete user with confirmation
162+
163+ ### Legacy Endpoints (Backward Compatibility)
164+ ** Note:** These endpoints default to V1 behavior for backward compatibility
134165- ` POST /api/auth/register ` - Register new user
135166- ` POST /api/auth/login ` - Login user
136167- ` POST /api/auth/logout ` - Logout user
137-
138- ### Users (Protected)
139168- ` GET /api/users ` - Get all users (paginated)
140169- ` GET /api/users/{id} ` - Get user by ID
141170- ` POST /api/users ` - Create user
142171- ` PUT /api/users/{id} ` - Update user
143172- ` DELETE /api/users/{id} ` - Delete user
144-
145- ### Files (Protected)
146173- ` POST /api/files/upload ` - Upload file
147174- ` DELETE /api/files/{id} ` - Delete file
148-
149- ### System
150175- ` GET /api/health ` - Health check
151176- ` GET /api/health/info ` - System info
152177
153178### Example Usage
154179``` bash
155- # Register
156- curl -X POST http://localhost:8000/api/auth/register \
180+ # V1 API (Explicit versioning - Recommended)
181+ curl -X POST http://localhost:8000/api/v1/ auth/register \
157182 -H " Content-Type: application/json" \
158183 -d ' {"name":"John Doe","email":"john@example.com","password":"password123"}'
159184
160- # Login
161- curl -X POST http://localhost:8000/api/auth/login \
185+ # V2 API (Enhanced responses)
186+ curl -X POST http://localhost:8000/api/v2/ auth/register \
162187 -H " Content-Type: application/json" \
163- -d ' {"email":"john@example.com","password":"password123"}'
188+ -d ' {"name":"John Doe","email":"john@example.com","password":"password123"}'
189+
190+ # Legacy API (Backward compatibility)
191+ curl -X POST http://localhost:8000/api/auth/register \
192+ -H " Content-Type: application/json" \
193+ -d ' {"name":"John Doe","email":"john@example.com","password":"password123"}'
164194
165- # Get users (with token)
195+ # Version via header
166196curl -X GET http://localhost:8000/api/users \
167- -H " Authorization: Bearer YOUR_JWT_TOKEN"
197+ -H " Authorization: Bearer YOUR_JWT_TOKEN" \
198+ -H " X-API-Version: v2"
168199```
169200
170201## 🗄️ Database Schema
@@ -259,6 +290,98 @@ timer_stop('api_call');
259290### Test Debug Bar
260291Visit ` http://localhost:8000/welcome ` to see the debug bar in action.
261292
293+ ## 🔄 API Versioning
294+
295+ The framework supports multiple API versions with backward compatibility and flexible version detection.
296+
297+ ### Version Detection Methods
298+
299+ 1 . ** URI Path** (Recommended)
300+ ``` bash
301+ GET /api/v1/users
302+ GET /api/v2/users
303+ ```
304+
305+ 2 . ** X-API-Version Header**
306+ ``` bash
307+ curl -H " X-API-Version: v2" http://localhost:8000/api/users
308+ ```
309+
310+ 3 . ** Accept Header**
311+ ``` bash
312+ curl -H " Accept: application/vnd.api+json;version=2" http://localhost:8000/api/users
313+ ```
314+
315+ ### Available Versions
316+
317+ #### Version 1 (v1)
318+ - Standard JSON responses
319+ - Basic error handling
320+ - Simple data structure
321+
322+ ``` json
323+ {
324+ "status" : " success" ,
325+ "data" : {... },
326+ "version" : " v1"
327+ }
328+ ```
329+
330+ #### Version 2 (v2)
331+ - Enhanced response format
332+ - Structured error codes
333+ - Metadata inclusion
334+ - Timestamp tracking
335+
336+ ``` json
337+ {
338+ "success" : true ,
339+ "data" : {... },
340+ "meta" : {
341+ "version" : " v2" ,
342+ "timestamp" : " 2024-10-21T10:30:00+00:00" ,
343+ "action" : " created"
344+ }
345+ }
346+ ```
347+
348+ ### Version-Specific Features
349+
350+ ** V1 Features:**
351+ - Basic CRUD operations
352+ - Simple response format
353+ - Standard HTTP status codes
354+
355+ ** V2 Features:**
356+ - Enhanced error handling with error codes
357+ - Metadata in responses
358+ - Improved pagination info
359+ - Structured error responses
360+
361+ ### Creating New Versions
362+
363+ 1 . Create version directory: ` app/controllers/v3/ `
364+ 2 . Create versioned controllers
365+ 3 . Add route file: ` app/routes/api_v3.php `
366+ 4 . Update Application.php to load new routes
367+
368+ ### Migration Strategy
369+
370+ ** For New Projects:**
371+ - Use explicit versioning from the start: ` /api/v1/ `
372+ - Avoid legacy endpoints
373+
374+ ** For Existing Projects:**
375+ - Legacy endpoints (` /api/ ` ) remain unchanged
376+ - Gradually migrate clients to versioned endpoints
377+ - Deprecate legacy endpoints in future versions
378+
379+ ** Best Practices:**
380+ - Always specify version in new integrations
381+ - Use semantic versioning for major changes
382+ - Maintain at least 2 versions simultaneously
383+ - Provide migration guides for version changes
384+
262385## 💻 CLI Support
263386
264387The framework includes a powerful command-line interface for development tasks.
0 commit comments