DEV Community

Cover image for How organize big projects in Laravel?
AbdlrahmanSaber
AbdlrahmanSaber

Posted on • Originally published at abdlrahmansaber.Medium

How organize big projects in Laravel?

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

 

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.

 

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.

 

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.

 

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.

 

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

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

 

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.

 

Cons

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

 

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

 

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, Twitter

Top comments (0)