PHPackages                             sethsandaru/laravel-hmvc-sample - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Framework](/categories/framework)
4. /
5. sethsandaru/laravel-hmvc-sample

ActiveProject[Framework](/categories/framework)

sethsandaru/laravel-hmvc-sample
===============================

Laravel HMVC structure sample project.

v1.0.0(7y ago)296513[1 issues](https://github.com/sethsandaru/laravel-hmvc-sample/issues)MITPHPPHP ^7.1.3

Since Oct 17Pushed 4y agoCompare

[ Source](https://github.com/sethsandaru/laravel-hmvc-sample)[ Packagist](https://packagist.org/packages/sethsandaru/laravel-hmvc-sample)[ RSS](/packages/sethsandaru-laravel-hmvc-sample/feed)WikiDiscussions master Synced today

READMEChangelogDependencies (9)Versions (3)Used By (0)

Laravel HMVC Structure - Sample Project
=======================================

[](#laravel-hmvc-structure---sample-project)

A sample project, building and using HMVC structure for Laravel 5.7+, 6, 7, 8.

What is HMVC?
-------------

[](#what-is-hmvc)

You can find out here: [HMVC - WIKI](https://en.wikipedia.org/wiki/Hierarchical_model%E2%80%93view%E2%80%93controller)

Key advantages (M.O.R.E):

- 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 work load.
- Usability: By nature of the design it is easy to reuse nearly every piece of code.
- Extensibility: Makes the application more extensible without sacrificing ease of maintenance.

### Other advantages

[](#other-advantages)

- Able to achieve Microservice with ease. Splitting up the Modules into Services would be blazing fast.
- In the Project itself it's like a Mono-Repo strategy.

Laravel HMVC Structure - this project
-------------------------------------

[](#laravel-hmvc-structure---this-project)

- Top - Base MVC - Laravel App
    - Module 1 MVC - Small Laravel App
    - Module 2 MVC - Small Laravel App
    - ...
    - Module n MVC - Small Laravel App

Module Structure
----------------

[](#module-structure)

- ModuleName
    - Configs: Your config files here.
    - Http:
        - Controllers: Your controllers here.
        - Requests
    - Languages: your language files here (translation)
        - en
        - vi
        - ...
    - Libraries: your special library classes file here.
    - Models: your models here.
    - Views: your view file here.
    - Routes: all the routes of the module
        - routes.php: routes of this module.
    - Services: your service handlers here
    - Providers: Service Provider
    - Policies: policies of the module

So you can see, a Module is just a small Laravel Application. You don't have to create a full structure to make it work.

The most important part is: `ServiceProvider`. Without that we can't boot up the Module.

How to create a new module?
---------------------------

[](#how-to-create-a-new-module)

1. Create your module folder inside `app/Modules`
2. Inside your module folder, create folder that you need like the structure above (ServiceProvider, Views,...)
3. Create a ServiceProvider and register it in `configs/app.php`
4. Test your module

How to access View/Language/Config?
-----------------------------------

[](#how-to-access-viewlanguageconfig)

Module folder name is the alias name to let us load views, language text,...
Example: I have **Home** module. Alias: `home`

### Load view

[](#load-view)

We need to insert module alias in the beginning like this:

```
return view('home::home.view', $arrData);
```

### Get language (translation) text

[](#get-language-translation-text)

```
trans('home::home.hello'); // Hello
__('home::home.world'); // World
```

#### Get Config Value

[](#get-config-value)

Just like normal config, you can get your config via your alias like this:

```
config('home.text'); // get 'text' in home alias
```

Migrations
----------

[](#migrations)

### How it works?

[](#how-it-works)

When you run `php artisan migrate`, it will run like this:

```
- Base/Core (database/migrations) first
- Thought each module
    - Module 1
    - Module 2
    - ...
    - Module n
```

### Create migration

[](#create-migration)

Just create migration file (Laravel format) in `/Migrations`.

Note:

- Filename must be: `yyyy_MM_dd_hhmmss_class_name_here.php` (follow Laravel format)

Console Command registration
----------------------------

[](#console-command-registration)

- Create your command in `app/Modules/
