Home Arrow Icon Knowledge base Arrow Icon Global Arrow Icon How do I handle namespace changes when migrating to nWidart/laravel-modules


How do I handle namespace changes when migrating to nWidart/laravel-modules


When migrating to `nwidart/laravel-modules`, you need to handle namespace changes to ensure that your modules are properly configured and recognized by the package. Here are the steps to handle namespace changes:

1. Update Module Namespaces:
- Open the `module.json` file for each module and update the namespace to match the new structure. For example, if you have a module named `Customers` in the `Modules` folder, update the namespace to `Modules\Core\Customers`.

2. Update Module Paths:
- In the `module.php` configuration file, update the `paths` key to include the new paths. For example, if you have moved your modules to `Modules/Core`, update the `paths` key as follows:

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

3. Update Composer Configuration:
- In the `composer.json` file, update the `autoload` section to include the new namespace. For example, if you have updated the namespace for the `Customers` module, add the following configuration:

json
     "autoload": {
         "psr-4": {
             "Modules\\Core\\Customers\\": "Modules/Core/Customers/src"
         }
     },
     

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

5. Update Module Configuration:
- If you have any custom configuration files for your modules, update them to reflect the new namespace.

Example Configuration:

php
<?php

return [
    'namespace' => 'Modules',
    'stubs' => base_path('Modules/Stubs'),
    'paths' => [
        'modules' => base_path('Modules/Core'),
    ],
    'composer' => [
        'merge-plugin' => [
            'include' => ['Modules/*/composer.json'],
        ],
    ],
    'cache' => true,
    'register' => [
        'Modules\Core',
    ],
];

Explanation:

- Namespace: Sets the default namespace for generated modules.
- Stubs: Specifies the directory where the package's stubs are located.
- Paths: Specifies the directories where your modules are located.
- Composer: Configures the `composer.json` file to include the module files.
- Cache: Enables caching of module information.
- Register: Specifies which custom namespaces need to be registered by the package.

By following these steps, you can ensure that your modules are properly configured and recognized by the `nwidart/laravel-modules` package, even after namespace changes.

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