Skip to content

Load modules from extra locations

To load modules stored outside paths.modules (for example in plugins/), add those folders to scan.paths. Each folder holds modules as subdirectories, such as plugins/Acme/module.json.

  1. Publish config/modules.php if you have not already.

    Terminal window
    php artisan vendor:publish --provider="Nwidart\Modules\LaravelModulesServiceProvider" --tag=config
  2. Enable scanning and list the extra folders.

    config/modules.php
    'scan' => [
    'enabled' => true,
    'paths' => [
    base_path('plugins'),
    ],
    ],

    The package appends /* to any path that does not already end with /*, then looks for module.json inside each match. base_path('plugins') matches plugins/Acme/module.json, while the default base_path('vendor/*/*') matches vendor/acme/blog/module.json.

  3. Add the folder to extra.merge-plugin.include in composer.json and rebuild the autoloader.

    composer.json
    "extra": {
    "merge-plugin": {
    "include": [
    "Modules/*/composer.json",
    "plugins/*/composer.json"
    ]
    }
    }
    Terminal window
    composer dump-autoload

    Autoload the module before enabling it. Enabling a module whose provider class cannot autoload breaks every artisan command with a class-not-found error.

  4. Enable the discovered module. Newly scanned modules start disabled.

    Terminal window
    php artisan module:enable Acme
  5. Check the result.

    Terminal window
    php artisan module:list

    Acme appears with its path (plugins/Acme [0]).

Call Module::addLocation() to expose an extra folder at runtime:

app/Providers/AppServiceProvider.php
use Nwidart\Modules\Facades\Module;
public function register(): void
{
Module::addLocation(base_path('plugins'));
}

addLocation() makes modules visible to Module::all(), Module::find(), and module:list. It does not register their service providers, because module providers load before application providers run. Use scan.paths to boot providers from extra folders.