How can I make a good structure in laravel project ?

  • 1 month ago
  • PHP
  • By Salma Mehanny
  • 39 Visits

How can I make a good structure in laravel project ?

Everyone, when beginning a new project, make confused if how can I make the project with the best approach what make me comfortable with the project and easy to get the files that I search
we have many approaches and design patterns that make the project clean
but in this article, I will take about the approach that calls #HMVC ( the Hierarchical Model View Controller(HMVC) pattern ).
HMVC is simply MVC but in the component-ordered base. Assume you have a website that has a blog and a forum part. HMVC divides these two parts into MVC components so you can have each of them separately developed but used as one!
For example, if we have a product, category, and order we build every model as a separate project it is called a module.
As Example from a real e-commerce project

Difference Between when using HMVC and when not using in routes

when using hmvc we can get the routes easily and search with route what we want


But when we use all routes in the application in the same file get this

Pros

Key advantages to implementing the HMVC pattern in your development cycle:

Modularization: Reduction of dependencies between the disparate parts of the application.
Organization: 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 more packages with laravel that generate modules but I will share in this article the best packages that I used in more projects and get it easy and a piece of cake


[https://nwidart.com/laravel-modules/v6/introduction]


nwidart/laravel-modules is a Laravel package that was created to manage your large Laravel app using modules. A module is like a Laravel package, it has some views, controllers, or models.

Quick Example

Generate your first module using php artisan module:make Blog

And Here we get all commands that help us to build Complete module
[https://nwidart.com/laravel-modules/v6/advanced-tools/artisan-commands]

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.