Hmvc
Understanding our Hierarchical Model-View-Controller architecture pattern.
HMVC Architecture
This document explains the Hierarchical Model-View-Controller (HMVC) architecture used in our application.
What is HMVC?
HMVC is an evolution of the MVC pattern that organizes code into hierarchical modules. Each module contains its own MVC triad, making the application more modular and maintainable.
Benefits of HMVC
-
Modularity
- Self-contained components
- Easy to maintain and update
- Reusable across projects
-
Scalability
- Horizontal scaling
- Easy to add new features
- Better code organization
-
Code Reusability
- DRY (Don't Repeat Yourself)
- Shared components
- Module independence
Module Structure
Each HMVC module contains:
Module/
├── Controllers/ # Handle requests and responses
├── Models/ # Data and business logic
├── Views/ # Presentation layer
├── routes.php # Module routes
├── config/ # Module configuration
└── tests/ # Module tests
Communication Between Modules
-
Direct Communication
// Example of calling another module app(OtherModule::class)->someMethod(); -
Event-Based Communication
// Firing events event(new ModuleEvent($data)); -
Service Layer
// Using service classes public function __construct(ModuleService $service) { $this->service = $service; }
Best Practices
-
Module Independence
- Keep modules loosely coupled
- Use interfaces for communication
- Implement clear boundaries
-
Consistent Structure
- Follow naming conventions
- Maintain similar structure across modules
- Document module interfaces
-
Testing Strategy
- Unit test each module independently
- Integration tests between modules
- End-to-end testing for critical paths