Laravel Module Management

What is Module Management in Laravel?

Let's imagine that you are working on a bigger scale application where you have to manage lots of features. You are managing multiple clients and your clients might have different requirements.

It would be hard to write custom code for each client rather you can create different types of modules and enable this module for only required client. That way you do not have to worry about writing custom code.

Another big advatange of using modular approach is that it comes with folder structure your feature is organized in nice directory structure as below:

App
|- Modules
   |-- Comments
      |-- Controllers
      |-- Models
      |-- Views
      |-- Migrations
         |-- Seeder
      |-- Translations
         |-- en
             |-- general.php
         |-- fr
             |-- general.php
      |-- config.php
      |-- routes.php

You have your feature specific classes, models, configs, translations etc.. which would not overlap with other modules and there for single place for your feature. You do not have to worry about your code affecting other modules because modules are isolated.

It would be easier to copy the whole module and integrate easily with new projects rather then copying line by line code from different files.

Laravel module management plugin

To create such modular application using laravel framework I created a small library that you can load via composer and can create different modules in your laravel app.

  • Laravel 6.0  (composer create-project learn2torials/laravel-modular)
  • Laravel 8.0  (composer create-project learn2torials/modular-laravel)

Depending on version of laravel you are using choose one of the above plugin.

Let's open a terminal window while being in your laravel root directory and run following command to install my plugin for module management:

# install composer package <= Laravel 6.0
composer create-project learn2torials/laravel-modular

# install composer package for laravel 8.0
composer create-project learn2torials/modular-laravel

# import config file from vendor to config file
# if you do not see modules.php file in config folder
# create a new file in config folder called modules.php
php artisan vendor:publish

# create a new module using command line
php artisan make:module comments

# run module migrations
php artisan migrate

# run module seeders
php artisan db:seed --class="L2T\Modular\Database\Seeder"

How to create laravel module?

Say you are working on blog website and you have to create a post module that deals with all functionality related to posts. Open your laravel project and run following command in your project root directory:

php artisan make:module post

Above command would generate following directory structure.

App
|- Modules
   |-- Comments
      |-- Controllers
      |-- Models
      |-- Views
      |-- Migrations
         |-- Seeder
      |-- Translations
         |-- en
             |-- general.php
         |-- fr
             |-- general.php
      |-- config.php
      |-- routes.php

Now, that we have generated folder structre we would need to add configuration file where we can turn on our modules. Open terminal and run following command to import config file:

php artisan vendor:publish

Above command will add a modular.php file in config folder. The file would look like following:

<?php

/*
|--------------------------------------------------------------------------
| Configuration File
|--------------------------------------------------------------------------
|
| You can overwrite default configuration here according to your app requirements.
|
*/
return [
    "i18n"     => false,
    "https"    => false,
    "modules"  => [

    ]
];

Each module has their config.php and routes.php files. You can define your run time configurations related to your post modules in config.php file.

You can define all of your posts related routes in routes.php file. Define all the controllers, models, migrations, translations etc.. related only to your post module in app/Modules/Post folder.

Create a demo post module page

For our demo purpose we will create one sample page related to our post module to see if it worked as we thought.

Let's create a sample route in app/Modules/Post/routes.php file as shown below:

Route::get('/posts','HomeController@index')->name('post');

Let's create a our HomeController and blade view file for our demo. Let's create a controller file in app/Modules/Post/Controllers/HomeController.php:

<?php

namespace App\Modules\Post\Controllers;
use App\Http\Controllers\Controller;

class HomeController extends Controller
{
    public function index()
    {
        return view('post::index');
    }
}

Finally let's create a view file in app/Modules/Post/Views/post.blade.php file with following contents:

<h1>Sample Post Module</h1>

Now, we have to tell our laravel app that which modules should it load for our current app.

This is very important because if you are running multiple website and you want to enable some modules for one site and some for another site rather then loading them all in all sites.

Open configs/modular.php file and enable our post module with following declaration:

<?php

/*
|--------------------------------------------------------------------------
| Configuration File
|--------------------------------------------------------------------------
|
| You can overwrite default configuration here according to your app requirements.
|
*/
return [
    "i18n"     => false,
    "https"    => false,
    "modules"  => [
        'post' => true
    ]
];

That is it, we have successfully created our module page to demo this page on your browser run following command in your terminal window while being on laravel project root directory:

php artisan serve

Now, go to your browser and hit http://localhost:8000/posts url and you will see our post page.

There are lot more things you can do with this plugin. Followings are some of the things handled by this plugin:

  • localization
  • prefix for modules
  • custom migrations/seeders/controllers/models/providers etc...

Checkout the readme file for appropriate plugin: