DEV Community

Cover image for How to Implement Laravel Modules Structure with Nwidart Package
Code And Deploy
Code And Deploy

Posted on

How to Implement Laravel Modules Structure with Nwidart Package

Originally posted @ https://codeanddeploy.com visit and download the sample code:
https://codeanddeploy.com/blog/laravel/how-to-implement-laravel-modules-structure-with-nwidart-package

In this post, I will show you how to implement Laravel modules structure in with Nwidart package. This will help us to make our application clean and organized code that is easy to maintain and reusable to any modules within the application.

Some developers recommend implementing a Laravel modules because it comes with own controllers, model, resources and more. That help us not to break our application even if we remove one of our laravel module as long as not a base module that connect with other modules.

Codeanddeploy also using laravel module structures.

When using Laravel modules it will generate with the following:

  1. Config

  2. Console

  3. Database

  4. Entities

  5. Http

  6. Providers

  7. Resources

  8. Routes

  9. Tests

Now let's start installing our Laravel modules.

Step 1: Laravel Installation

If you don't have a Laravel 8 install in your local just run the following command below:



composer create-project --prefer-dist laravel/laravel laravel-modules-example

cd laravel-modules-example


Enter fullscreen mode Exit fullscreen mode

Step 2: Laravel Module Package Installation

Now, let's install our Laravel module package by Nwidart. Run the following command below:



composer require nwidart/laravel-modules


Enter fullscreen mode Exit fullscreen mode

Step 3: Publish Configuration File

Run the following command to publish the configuration file.



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


Enter fullscreen mode Exit fullscreen mode

Step 4: Setup Autoloading

Now, we need to add "Modules\\":"Modules/", to your composer under autoload > psr-4. See example below:



"autoload": {
    "psr-4": {
        "App\\": "app/",
        "Modules\\": "Modules/",
        "Database\\Factories\\": "database/factories/",
        "Database\\Seeders\\": "database/seeders/"
    }
},


Enter fullscreen mode Exit fullscreen mode

Then run the following command after setup above:



composer dump-autoload


Enter fullscreen mode Exit fullscreen mode

Now, we already setup our Laravel module package. Next let's create a module for our Laravel application.

Step 5: Create Module

Syntax of create module command:



php artisan make:module module_name


Enter fullscreen mode Exit fullscreen mode

Then run the following command to create module let's do an example for Posts module.



php artisan make:module posts


Enter fullscreen mode Exit fullscreen mode

After running above commands it will generate our Posts module under Modules folder. See below Laravel module structures:



app/
bootstrap/
vendor/
Modules/
  ├── Posts/
      ├── Assets/
      ├── Config/
      ├── Console/
      ├── Database/
          ├── Migrations/
          ├── Seeders/
      ├── Entities/
      ├── Http/
          ├── Controllers/
          ├── Middleware/
          ├── Requests/
      ├── Providers/
          ├── PostsServiceProvider.php
          ├── RouteServiceProvider.php
      ├── Resources/
          ├── assets/
              ├── js/
                ├── app.js
              ├── sass/
                ├── app.scss
          ├── lang/
          ├── views/
      ├── Routes/
          ├── api.php
          ├── web.php
      ├── Repositories/
      ├── Tests/
      ├── composer.json
      ├── module.json
      ├── package.json
      ├── webpack.mix.js


Enter fullscreen mode Exit fullscreen mode

Now, we successfully generated our Posts module. Let's test it by running the command below:



php artisan serve


Enter fullscreen mode Exit fullscreen mode

Then run the URL to your browser:



http://127.0.0.1:8000/posts


Enter fullscreen mode Exit fullscreen mode

Then you will see the result below:

dg

For more details about it click Laravel modules.

I hope this tutorial can help you. Kindly visit here https://codeanddeploy.com/blog/laravel/how-to-implement-laravel-modules-structure-with-nwidart-package if you want to download this code.

Happy coding :)

Top comments (4)

Collapse
 
kaythinks profile image
kaythinks

The command to generate a module is php artisan module:make module_name not php artisan make:module module_name

Collapse
 
patrickmaciel profile image
Patrick Maciel

This works on Laravel 8+? Because in docs say 5.8

Collapse
 
codeanddeploy profile image
Code And Deploy

Yes.. still using it in Laravel 9.

Collapse
 
dcblog profile image
David Carr

I'm also using it with Laravel 10