DEV Community

Cover image for How can I make a good structure in laravel project ?

Posted on

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

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

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

Alt Text


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.


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


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


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.

Top comments (7)

lito profile image

I'm using a domain design in my projects.

The app folder contains the Domains folder with all related domains. Every Model is a domain (and some other non models contents like Dashboard).

Domain Scaffolding

Every domain include his own router.php like app/Domains/Exchange/Controller/router.php:

<?php declare(strict_types=1);

namespace App\Domains\Exchange\Controller;

use Illuminate\Support\Facades\Route;

Route::group(['middleware' => ['user.auth']], static function () {
    Route::get('/exchange', Index::class)->name('exchange.index');
    Route::get('/exchange/{product_id}', Detail::class)->name('exchange.detail');
Enter fullscreen mode Exit fullscreen mode

All routers are loaded from a main app/Http/router.php file:

<?php declare(strict_types=1);

foreach (glob(app_path('Domains/*/Controller*/router.php')) as $file) {
    require $file;
Enter fullscreen mode Exit fullscreen mode

Domain Controller folder is the default Controller type on project, sometimes API and sometimes Web. Also you can create more alternative controllers folders like ControllerApi and ControllerWeb.

Also every domain include his own resources/views/domain folder:

Domain Scaffolding

And this is my point of view :)

muzammilaalpha profile image

Nice one!

mohamed7sameer profile image


ayman_elmalah profile image
Ayman Elmalah

Good job, it's a great and helpful article

salmazz profile image

Thank u :))

progalaa profile image
Alaa A.Sayed

Good, but also you can divide routes to separate file every file related to it's module and map them in RouteServiceProvider

salmazz profile image

yes it is also a good idea but HMVC arranges everything, not the routes only