This directory contains examples demonstrating different approaches to building REST API endpoints in MikoPBX modules.
ModuleExampleRestAPIv1 - Pattern 1: Simple Callback
Status: ✅ Production-ready, legacy pattern
The simplest approach using a single callback method for all API requests.
Pros:
- Minimal code
- Easy to understand
- Good for simple modules with 1-2 endpoints
Cons:
- No automatic OpenAPI documentation
- Manual routing logic
- Harder to maintain with many endpoints
Use when: You have a simple module with just a few API endpoints
ModuleExampleRestAPIv2 - Pattern 2: Custom Controllers
Status: ✅ Production-ready, transitional pattern
Intermediate approach with custom controllers but manual routing.
Pros:
- Better code organization than v1
- Separation of concerns
- Reusable controllers
Cons:
- Still requires manual route registration
- No automatic OpenAPI spec generation
- More boilerplate than v3
Use when: Migrating from v1 to v3, or need custom routing logic
ModuleExampleRestAPIv3 - Pattern 3: Modern 2025 ⭐
Status: ✅ Recommended, production-ready
Modern approach using PHP 8.3 attributes for automatic routing and OpenAPI generation.
Pros:
- Zero-configuration routing with PHP attributes
- Automatic OpenAPI 3.1.0 spec generation
- Full integration with MikoPBX API registry
- Clean, maintainable code structure
- Follows 2025 best practices
Cons:
- Requires PHP 8.3+ (not a problem for MikoPBX)
- Slightly more initial setup
Use when: Starting a new module (default choice for all new development)
| Feature | v1 (Callback) | v2 (Controllers) | v3 (Attributes) ⭐ |
|---|---|---|---|
| OpenAPI Spec | ❌ Manual | ❌ Manual | ✅ Automatic |
| Routing | ❌ Manual | ❌ Manual | ✅ Automatic |
| Code Organization | ✅ Good | ✅ Excellent | |
| Maintainability | ✅ Medium | ✅ High | |
| Learning Curve | ✅ Easy | ||
| Recommended For | Legacy, 1-2 endpoints | Migration, custom routing | All new development |
# Modern approach (recommended)
cd ModuleExampleRestAPIv3
# Legacy approach (simple modules)
cd ModuleExampleRestAPIv1
# Transitional approach (migration)
cd ModuleExampleRestAPIv2Each example includes:
- Complete working code
- Detailed README with implementation guide
- Comments explaining WHY, not just WHAT
- Test examples
Use the example as a template for your own module development.
- v1 Implementation Guide - Simple callback pattern
- v2 Implementation Guide - Custom controllers
- v3 Implementation Guide - Modern attributes ⭐
→ Use v1 for minimal overhead
→ Use v2 as intermediate step, then move to v3
→ Use v3 directly ⭐
→ Use v2 for full control over routing
The v3 pattern implements a standardized 7-phase lifecycle:
- Validation - Input validation and sanitization
- Authentication - User/token verification
- Authorization - Permission checks
- Business Logic - Core operation execution
- Data Transformation - Format response data
- Response - Send HTTP response
- Cleanup - Release resources
See ModuleExampleRestAPIv3 README for detailed explanation.
v1 (Callback)
↓
v2 (Controllers) ← Transitional step
↓
v3 (Attributes) ⭐ ← Modern target
Recommendation: New modules should start with v3 directly.
⭐ TL;DR: Use ModuleExampleRestAPIv3 for all new development