Advantages of using HMVC?
What’s wrong with the MVC pattern?
Let’s know how we can use the HMVC pattern in Laravel!
Best Packages that generate modules and implement that approach
Everyone when beginning a new project starts to ask himself/herself the following question?
- How can I make organized the project well to be easy for me or for any other developer easy to understand for example if you’re looking for a file in a big project, you want to be easy to find that file, right?
We have many approaches and design patterns that make the project clean but in this article, I will talk about HMVC (the Hierarchical-model-view-controller) pattern which makes your application modular.
The HMVC pattern extends the MVC pattern and is a solution to overcome scalability problems apparent within large MVC applications.
An HMVC application operates using a collection of MVC entities where each MVC entity can execute without the presence of any other. Ideally, each entity should never load models or libraries of other entities.
HMVC supports the reuse of code
HMVC allows easy distribution of MVC entities stored in a single directory.
HMVC reduces dependencies and allows to easily extend applications while retaining ease of maintenance.
Testing is easier because the system is divided to a large extend into independent parts.
The Model-View-Controller (MVC) paradigm remains one of the more enduring patterns. However, the traditional MVC scope falls short when it comes to the control of GUI elements (widgets). MVC does not handle the complexities of data management, event management, and application flows.
HMVC provides a powerful yet easy-to-understand layered design methodology for developing a complete presentation layer.
Let’s assume we have an accounting project that has (Banking — Sale— Transaction), the question now is how we can organize the project using HMVC! let’s divide these three parts into MVC components so you can have each of them separately developed but used as one!
Example from a real accounting project
Each module has its own (routes - middleware - controllers- etc..), and it’s help you to make your project more organized.
Modularization: reduction of dependencies between the disparate parts of the application.
More organized: having a folder for each of the relevant triads makes for a lighter workload.
Reusability: by the nature of the design it is easy to reuse nearly every piece of code.
Extendibility: this makes the application more extensible without sacrificing ease of maintenance.
Create More Folders But the solution of it in my opinion that we will remove unused folders.
we have many packages with Laravel that generate modules but I will share in this article my own package (Laragine) and other packages that I used in many projects.
Is a Laravel package that was created to manage your large Laravel app using modules.
This package just structures your project (Modules) and doesn’t provide any implementation.
is a Laravel package that was created to manage your large Laravel app using modules also but here’s a difference, this package implements all API CRUD operations with unit tests, the package generates (Controller — Model — Factories — Migrations — unit tests — etc..), all you need to do to just add your API route in your route folder and everything (APIs CRUD operations) will work magically sounds interesting, right!
check this video to know more.
I wrote a series of blogs about it you can check it from here https://dev.to/abdlrahmansaberabdo/series/14891
Check the documentation
Basically, the HMVC pattern is just an extension of MVC. An HMVC application includes one or more MVC sub-applications. so anything MVC can do, HMVC can do too. Now, it depends on whether you need the flexibility and scalability that HMVC offers.