Back to Blog

Laravel HMVC | Steps to Create HMVC Structure | Module Wise

Custom Own HMVC Basic Structure with Laravel Inside this blog, you will learn and be able to create your own custom basic HMVC structure that will help you to manage feature-wise separate code base and easily can be integrated inside another project in case the same module or feature exists on another project Let’s start […]

Laravel HMVC | Steps to Create HMVC Structure | Module Wise

Custom Own HMVC Basic Structure with Laravel

Inside this blog, you will learn and be able to create your own custom basic HMVC structure that will help you to manage feature-wise separate code base and easily can be integrated inside another project in case the same module or feature exists on another project


Let's start to understand and make HMVC Structure in Laravel 5.3 Version,

Most important thing in the HMVC structure is the namespace:

In any case, you changes in your file and that won't affect, you need to fire the below artisan command :

php artisan config:cache

 

 

Create a module.php file inside the config directory. Paste below text:

 

<?php
# config/module.php
return [
    'modules' => [
        'Module_name_first',
        'Module_Name_second',
    ]
];

Now, create a new directory named Modules inside the root level of your project, and this directory will store all the modules that are used inside the project.

Then we have to define our own service provider class to load and boot our all modules' routes, views, controllers, and other things from each module.

Create a service provider, named ModuleServiceProvider.php inside our modules directory:

To create a service run below artisan command below:

php artisan make:provider ModuleServiceProvider

And now move the ModuleServiceProvider.php to our Modules directory and change the namespace to namespace Modules; and update boot() from the below:

public function boot() {
  // For each of the registered modules, include their routes and Views
  $modules = config("module.modules");

  while (list(,$module) = each($modules)) {

    // Load the routes for each of the modules
    if(file_exists(__DIR__.'/'.$module.'/routes.php')){
    	include __DIR__.'/'.$module.'/routes.php';
    }

    // Load the views
    if(is_dir(__DIR__.'/'.$module.'/Views')) {
		$this->loadViewsFrom(__DIR__.'/'.$module.'/Views', $module);
    }//End If

  }//END LOOP
  
}//End Function

 

View Below Image :
laravel-mvc-service-provider


Register this service provider in the 
config/app.php file adding the service provider name to the provider variable.

laravel-mvc-config-app

You also need to modify
composer.json file, find AUTOLOAD and in this, you will see PSR-4.

now add another element - 
“Module//” : ”Module/” 

see in below image :
laravel-mvc-composer.png


Yeah! you have completed a setup for the module and now you will create all the modules and related module files in the main module directory.


You can see in the last image on the left side there are category modules and all the controllers, views, routes, and models are defined in the category module.


Whenever you create a new module, you just only copy your first module and paste 
it. But remember to add your module name to config/module.php file and about the namespace.