Laravel 5.5 HMVC modular structure

Laravel is the amazing framework. However when it comes to writing more complex and bigger application easily. Nowadays the Laravel is mostly used PHP framework for developing complex and bigger application. The default Laravel structure comes with cumbersome and not ideal.

On the default, Laravel structure is in the “app/” folder. every controller and models files are stored in the “app/” folder. When we developed the bigger application on Laravel have a more than 35 to 45 masters at that time it difficult to manage it with default Laravel structure.

The modular structure is very helpful for large-scale application. Let’s see how to set HMVC modular structure on Laravel.

I hope you installed the Laravel if not please follow this tutorial.

Now Let’s start to install package for module management in Laravel through composer.

Fire composer command from the root of the Laravel.

composer require nwidart/laravel-modules

Now, wait for some moment till installing the Laravel package.

After installing the laravel package, First, we need to register service Provider. Let’s open “config/app.php” file add service provider.

'providers' => [
    Nwidart\Modules\LaravelModulesServiceProvider::class,
],

After, register service provider let’s publish the laravel module configuration file.

php artisan vendor:publish --provider="Nwidart\Modules\LaravelModulesServiceProvider"

Now, open the “composer.json” file for autoloading your modules we need to register modules directory like.

{
  "autoload": {
    "psr-4": {
      "App\\": "app/",
      "Modules\\": "Modules/"
    }
  }
}

don’t forget to run composer dump-autoload afterward.

Configuration is done for Laravel module management package. Let’s let’s create the first “Hello Word” Module.

For creating the new modules fire below artisan command.

php artisan module:make Helloword

The modules are created let’s check in “modules” directory the “Helloword” module is created.

Now, first check the route prefix of your module then run the module URL.

<?php

Route::group(['middleware' => 'web', 'prefix' => 'helloword', 'namespace' => 'Modules\Helloword\Http\Controllers'], function()
{
    Route::get('/', 'HellowordController@index');
});

My module route prefix is helloword let’s run the module.

http://localhost:8000/helloword

The output of the first Helloword module is something like

Hello World
This view is loaded from module: Helloword

The first Helloword Laravel module management is ready for used.

if you face any issue with modular structure write the comment below.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s