What is module management?

If you are working on bigger scale applications sometime it is very difficult to manage features. Say your client has multiple websites and they all use some of the common features.

It would be pain when creating a new site you have to copy each file and functions from old code rather if your code were organized with features or module you can easily copy them to your new project or can share folders between different projects.

Modular apps organise code nicely it ties code related to particular feature. For example, admin module, auth module, post module etc..

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.

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
composer require phpcodebooster/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

Now let's enable new service provider that enables module management functionality. Open config/app.php file and add following line to providers array:

'providers' => [

    // Other Service Providers

We are all set with plugin setup now we have to learn how to create our first module.

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. To create such module we have to create following folder structure in our laravel app:

    └── Modules/
        └── Post/
            ├── Controllers/
            ├── Models/
            ├── Views/
            ├── Migrations/
            ├── Translations/
            ├── routes.php
            └── config.php  

Let's understand this structure, when laravel app loads it will go through all the modules defined under app/Modules folder and load them one by one.

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:


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:


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/modules.php file and enable our post module with following declaration:

return [
   'enabled' => [
       'post' => 'Post Module'  // make sure key is lowercase

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.

News and Updates

Subscribe to our newsletter and receive the latest news from TheThemeio.

Related Tags

Related Articles