CodeBriefly
Tech Magazine

Brief Note on Laravel Modular Approach

3 2,681

Get real time updates directly on you device, subscribe now.

In this article, we will discuss “Laravel Modular Approach”. We know Laravel is an amazing PHP framework, provides rich features and functionality, that make our development much easy.

I’m assuming you are familiar with the Laravel and MVC pattern. If Laravel is new for you, then you can start with our Laravel tutorials for the better understanding of this article.

Why we need Laravel Modular Approach?

We found some difficulties in Laravel file structure for a large project, where all our application logic available in “app/” folder. You need to think about it, All your project files inside the “app/” directory, each of the model placed in the root of the project. Sometimes it may be confusing to search out the specific file.

In Modular Approach, we create different modules for our Laravel application. Group of multiple modules helps to complete your application.

Now, the question is how to create a modular application in Laravel.

Here, I want to share a wonderful resource “nwidart/laravel-modules”. This will help us to manage the modules in our Laravel application. The module looks like a Laravel package, it contains separate MVC (Model, View, and Controllers). Means each module has a model, view, containers, and other files.

Laravel Modules package provides an easy way to manage all our modules easily. It will generate multiple artisan commands.

Install Laravel Modules Package

You can use the following command to install the package.

composer require nwidart/laravel-modules

Laravel Modular Approach

You need to publish the package configuration using the following command.

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

After executing the command, module configuration file located at “config/modules.php”.

You can use the following command to check the available new commands.

Laravel Modular Approach

Autoloading

You need to update the composer.json file. Open your file, and update the following code snippet.

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

After, updating the file. Don’t forget to execute the “composer dump-autoload” at the terminal.

Create a Blog Module

You can use the following artisan command to create your first module. In this example, we are creating the Blog Module.

php artisan module:make Blog

// Create multiple module using single command
php artisan module:make Blog, Shop

By default, Modules contains the controller, service provider, and other required files. If you don’t want to create those files, then you can use “–plain” or “-p” flags with your command.

php artisan module:make Blog -p

For example, the Blog Module

Laravel Modular Approach

After creating the module, New Modules directory created at the root and contains our newly creates Blog module.

Laravel Module Approach

Conclusion

In this article, we will discuss “Laravel Modular Approach”. We will discuss more on modular approach in our future articles such as Custom namespacing, Tools, Artisan commands, and many more. You can also get more details on the official documentation of  “nwidart/laravel-modules”. Please feel free to add any comment if any query or you can also send your feedback 🙂

If you like our content, please consider buying us a coffee.
Thank you for your support!
Buy Me a Coffee

Get real time updates directly on you device, subscribe now.

3 Comments
  1. Kriti says

    Thanks for sharing this 🙂

  2. Ajay says

    thanks for sharing this, keep it up…

  3. Sujai C says

    Pankaj, Does modular approach work with Laravel 7?

Leave A Reply

Your email address will not be published.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. AcceptRead More