Home Arrow Icon Knowledge base Arrow Icon Global Arrow Icon How do I migrate my existing Laravel app to use nWidart/laravel-modules


How do I migrate my existing Laravel app to use nWidart/laravel-modules


To migrate your existing Laravel app to use the `nwidart/laravel-modules` package, 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 Your First 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.

4. Structure and Files:
- The generated module structure will include directories for assets, config, console, database, migrations, seeders, entities, http (controllers, middleware, requests), providers, resources (views, lang), routes, repositories, and tests.

5. Upgrade to v6:
- If you are upgrading from a previous version, run the following command to migrate your modules to version 6:

bash
     php artisan module:v6:migrate
     

6. Move Existing Modules:
- If you have existing modules in the `Modules` folder, you can move them to a new subfolder structure. For example, you can move modules from `Modules` to `Modules/Core` and `Modules/External` as shown below:

bash
     Modules
     |___ Core
     |   |__ Customers
     |   |__ Products
     |___ External
         |__ Taller
     

7. Update Configuration:
- Update the `module.php` configuration file to include the new paths. For example, to set the modules path to `Modules/Core`, add the following configuration:

php
     'paths' => [
         'modules' => base_path('Modules/Core'),
     ],
     

8. Autoloading:
- To autoload your modules, add the following configuration to your `composer.json` file:

json
     "extra": {
         "laravel": {
             "dont-discover": [],
             "merge-plugin": {
                 "include": ["Modules/*/composer.json"]
             }
         }
     }
     

- Run `composer dump-autoload` to update the autoloading.

9. Namespace Changes:
- If you need to change the namespace of your modules, you can do so by updating the namespace in the `module.json` file and running `composer dump-autoload` again.

10. Testing:
- Use the `RefreshModuleDatabase` trait to speed up your unit testing by migrating only the relevant module's migration files.

By following these steps, you can effectively migrate your existing Laravel app to use the `nwidart/laravel-modules` package, which provides a structured way to organize your application into smaller, reusable modules[1][2][4].

Citations:
[1] https://nwidart.com/laravel-modules/v6/introduction
[2] https://stackoverflow.com/questions/74545403/laravel-nwidart-laravel-modules-move-an-existing-module-to-another-folder
[3] https://github.com/nWidart/laravel-modules/actions/runs/9539720431/job/26290392108
[4] https://github.com/nWidart/laravel-modules
[5] https://www.reddit.com/r/laravel/comments/tz69b0/refreshmoduledatabase_trait_for/