Feature Request: Enhance Routing in Hazaar MVC
Problem Statement
The current routing mechanism in Hazaar MVC lacks flexibility and consistency, leading to a mishmash of routing principles. While the existing router effectively determines which controller to load based on the request URL, the responsibility for routing actions to appropriate controller functions resides within the controllers themselves. This approach limits flexibility and makes it challenging to manage and extend routing functionality. There is a need to improve the routing system in Hazaar MVC to provide a more structured and configurable approach.
Proposed Solution
Enhance the routing system in Hazaar MVC to allow for greater flexibility and configurability while maintaining compatibility with existing applications. The proposed solution involves introducing a modular and configurable router architecture that separates routing logic from controller actions. This architecture enables the use of different routing strategies, such as configuration file-based routing, annotation-based routing, and more, without coupling routing logic with controller implementation details.
Implementation Details
-
Router Refactoring:
- Refactor the existing router class (e.g., Hazaar\Application\Router) to decouple routing logic from controller actions.
- Rename the router class to a more descriptive name (e.g., Hazaar\Application\Router\Basic) to differentiate it from other routers.
-
Modular Router Architecture:
- Design a modular router architecture that allows for the addition of multiple routers with different routing strategies.
- Implement a configuration-based router that loads routes from a configuration file (e.g., routes.json) for straightforward route management.
- Develop an annotation-based router that leverages PHP annotations to define routes, similar to the routing mechanism implemented in the Hazaar\Controller\REST class.
-
Action Routing Consolidation:
- Move action routing logic out of controllers and into the router components.
- Controllers become solely responsible for executing action functions without concerning themselves with routing details.
-
Configuration and Extensibility:
- Allow the router to be configured in the application configuration, enabling developers to specify which router to use and customize routing behavior.
- Provide hooks or interfaces for extending the router functionality to support custom routing strategies or third-party integrations.
Benefits
- Flexibility and Configurability: The enhanced routing system allows developers to choose from different routing strategies and customize routing behavior to suit application requirements.
- Modularity and Extensibility: The modular router architecture facilitates the addition of new routing strategies and extensions without disrupting existing code or functionality.
- Improved Code Organization: Separating routing logic from controller actions leads to cleaner and more maintainable code, with controllers focused solely on executing business logic.
Acceptance Criteria
- The routing system in Hazaar MVC is refactored to support a modular router architecture.
- Multiple router components, including configuration-based and annotation-based routers, are implemented to provide different routing strategies.
- Action routing logic is consolidated within the router components, removing the responsibility from controllers.
- The router is configurable via the application configuration, allowing developers to specify routing behavior and choose the appropriate router strategy.
Note
Enhancing the routing system in Hazaar MVC improves code organization, flexibility, and extensibility, enabling developers to build more robust and maintainable applications. Careful consideration and testing are essential to ensure backward compatibility and smooth migration for existing applications.