Home Arrow Icon Knowledge base Arrow Icon Global Arrow Icon How do I create a new module using nWidart/laravel-modules


How do I create a new module using nWidart/laravel-modules


To create a new module using `nwidart/laravel-modules`, follow these steps:

1. Install the Package:
- Run the following command to install the package via Composer:

bash
     composer require nwidart/laravel-modules
     

2. Publish the Configuration:
- Publish the package's configuration file by running:

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

3. Generate the Module:
- Use the command `php artisan module:make Blog` to generate a new module named "Blog". This will create the necessary directory structure and files for the module.

Example Directory Structure:

bash
app/
bootstrap/
vendor/
Modules/
Blog/
Assets/
Config/
Console/
Database/
Migrations/
Seeders/
Entities/
Http/
Controllers/
Middleware/
Requests/
Providers/
BlogServiceProvider.php
Resources/
lang/
views/
Repositories/
Tests/
composer.json
module.json
start.php

Explanation:

- Assets: Directory for module assets.
- Config: Directory for module configuration files.
- Console: Directory for module console commands.
- Database: Directory for module database migrations and seeders.
- Entities: Directory for module entities.
- Http: Directory for module HTTP controllers and middleware.
- Providers: Directory for module service providers.
- Resources: Directory for module views and lang files.
- Tests: Directory for module tests.

Additional Steps:

- Autoloading: Add the module to the autoloading configuration in `composer.json`:
json
     "autoload": {
         "psr-4": {
             "Modules\\Blog\\": "Modules/Blog/src"
         }
     }
     

- Run Composer Dump-Autoload: Run `composer dump-autoload` to refresh the autoloading.

By following these steps, you can effectively create and manage new modules in your Laravel application using the `nwidart/laravel-modules` package.

Citations:
[1] https://github.com/nWidart/laravel-modules/actions/runs/9539720431/job/26290392108
[2] https://blog.codehunger.in/how-to-create-custom-module-in-laravel-9/
[3] https://github.com/nWidart/laravel-modules
[4] https://nwidart.com/laravel-modules/v6/introduction
[5] https://stackoverflow.com/questions/36997571/how-to-create-new-module-in-laravel