Skip to content

Change where modules live and their namespace

To store modules in app-modules/ under the AppModules root namespace, update paths.modules and namespace in config/modules.php, plus the merge-plugin glob in composer.json. Publish config/modules.php first if you have not already:

Terminal window
php artisan vendor:publish --provider="Nwidart\Modules\LaravelModulesServiceProvider" --tag=config
  1. Update the namespace and modules path in config/modules.php.

    config/modules.php
    'namespace' => 'AppModules',
    'paths' => [
    'modules' => base_path('app-modules'),
    // ...
    ],
  2. Update the merge-plugin glob in composer.json so Composer autoloads classes from the new folder.

    composer.json
    "extra": {
    "merge-plugin": {
    "include": [
    "app-modules/*/composer.json"
    ]
    }
    }
  3. Rebuild the autoloader.

    Terminal window
    composer dump-autoload
  4. Create a module and verify its location and namespace.

    Terminal window
    php artisan module:make Blog

    This creates app-modules/Blog. Its composer.json maps AppModules\Blog\ to app/, and module.json registers AppModules\Blog\Providers\BlogServiceProvider.

Config changes do not move or rewrite existing modules. Migrate each existing module manually:

  1. Move the module folder.

    Terminal window
    mkdir -p app-modules
    mv Modules/Blog app-modules/Blog
  2. If you changed namespace, replace Modules\Blog (and escaped Modules\\Blog in module.json and composer.json) with AppModules\Blog. This command leaves Nwidart\Modules\... untouched:

    Terminal window
    grep -rl 'Modules' app-modules/Blog | xargs perl -pi -e 's/(?<!\w)(?<!\w\\)Modules(\\{1,2})Blog\b/AppModules$1Blog/g'
  3. Update the merge-plugin glob in composer.json as above and run composer dump-autoload.

  4. Check the result.

    Terminal window
    php artisan module:list

    Blog shows app-modules/Blog as its path. Its enabled status stays unchanged because modules_statuses.json is keyed by module name.

Update imports of the old namespace in your application code manually. module:composer-update does not rename namespaces; it only updates legacy Modules\<Name>\App\ PSR-4 entries.