How organize big projects in Laravel?
Tags: laravel, webdev, php, microservices
Published: 2022-04-19 08:20:41

## Agenda

* Introduction

* What’s HMVC?

* Advantages of using HMVC?

* What’s wrong with the MVC pattern?

* Let’s know how we can use the HMVC pattern in Laravel!

* Pros

* Cons

* Best Packages that generate modules and implement that approach

* Conclusion

&nbsp;

### **Introduction**

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.

&nbsp;

### **What’s HMVC?**

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.

&nbsp;

### **Advantages of using HMVC?**

* 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.

&nbsp;
### **What’s wrong with the MVC pattern?**

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.

&nbsp;
### **Let’s know how we can use the HMVC pattern in Laravel!**

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**

![laravel project example](https://cdn-images-1.medium.com/max/2000/1*Rwt1xel_eY15l-qwdsvOYw.png)

Each module has its own (routes - middleware - controllers- etc..), and it’s help you to make your project more organized.

&nbsp;
### **Pros**

* 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.

&nbsp;
### **Cons**

Create More Folders But the solution of it in my opinion that we will remove unused folders.

&nbsp;
### **Best Packages that generate modules and implement that approach**

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.

### **Laravel Modules**

* 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.

### **Laragine**

* 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 ](https://www.youtube.com/watch?v=5klckaSiM6g)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](https://yepwoo.com/products/laragine/docs/v2/introduction)

&nbsp;
### **Conclusion**

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.

Let’s connect on [LinkedIn](https://www.linkedin.com/in/abdlrahmansaber/), [Twitter](https://twitter.com/Abdlrahmansabe4)