Using Modular Approach With Laravel: Building Module Based ApplicationPART 1

Adedotun Bashorun
4 min readFeb 4, 2019

Modular Approach, subdividing your project business logic into different parts, that made up the whole project and this modules are reusable in other projects without much modification.

The Problem

Laravel is an amazing framework but when it comes to building a complex project the default laravel structure becomes cumbersome and not ideal. If you have worked on a large project before, you might have noticed that the logic in your controller becomes much and everything becomes choked up. At some point you might spend a lot of time looking for things because everything is bunched together.

Modular Approach To The Rescue

This is what the modular approach is trying to resolve, building each part of the project on its own but communicate with other modules in the project. i.e : you split each business logic of your project into different parts that communicate with each other.

Every module has it own model,view,controller,route meaning every module has a group of class that are related.

Example

Consider this default laravel project structure.

As you can see there is a lot of folder out else this would even be longer, the provider,repository,service class,views and more. so to prevent this mess here come the solution. A laravel package called ‘nwidart/laravel-modules’.

nwidart/laravel-modules is a laravel package which created to manage your large laravel project using modules. Module is like a laravel package, it has some views, controllers or models. This package is supported and tested in Laravel 5.

nwidart/laravel-modules package is a re-published, re-organised and maintained version of pingpong/modules, which isn’t maintained anymore.

With one big added bonus that the original package didn’t have: tests.

To crown it all, every module can be used as a composer package. Meaning you can re-use your modules on other projects and handle its versioning easily.

This means that on top of having a maintainable architecture, you also save time being able to re-use modules you created for other projects.

Please follow the step below to implement your first modular project.

Requirements

The modules package requires PHP 7.1 or higher. The Laravel package also requires Laravel 5.7 according to the documentation.

Installation and Setup

composer require nwidart/laravel-modules

The package will automatically register a service provider or alias. Optionally, you can publish the package configuration file.

php artisan vendor:publish — provider=”Nwidart\Modules\LaravelModulesServiceProvider”

Autoloading

In your composer.json file add the module class because modules are not loaded by default in laravel. For Example:

psr-4 autoload

Then run composer dump-autoload after finishing the above processes.

How to create a module

Creating a module is simple and straightforward. Run the following command to create a module.

php artisan module:make <module-name>

Replace the <module-name> with the desired module name you want e.g: php artisan module:make School

Note : please it is strongly recommended to use a PascalCase naming convention when naming your module. Because we are autoloading the modules using psr-4. This is according to the nwidart/laravel-modules documentation.

It is also possible to create multiple modules in one command.

php artisan module:make Cinema Showtime Movies

The command above will generate three different modules called Cinema , Showtime and Movies. After creating a module you should get something similar to the image displayed below:

modular structure

The ‘Modules’ folder contain the three modules created with this command, so you can go ahead and try yours and you will see your own modules listed as display in the image above.

It has been an exciting moment all along, to be able to use a modular approach when building your Laravel project for better management and use.

Summary

We have been able to learn how to use modular approach when building laravel project using nwidart/laravel-modules package, and why it is necessary to use the modular approach when building a large project.

You can try it out, and watch out for other parts where we will use these modules created in this tutorial to complete our laravel modular project.

Thank you for reading and please don’t forget to give a clap so that others can find this article easily.

--

--

Adedotun Bashorun

A Software Developer, php (Laravel), NodeJs(expressJs, koaJs, sailsJs), python(Django) enthusiast.