Skip to content

Publish a module as a Composer package

At the end, the Blog module is published as a Composer package (acme/blog) and loaded from vendor/acme/blog in another application. Prerequisites: a Blog module in Modules/Blog and a Git host.

  1. Move the module into its own Git repository. The folder already has module.json and composer.json at its root.

    Terminal window
    cp -R Modules/Blog ../blog
    cd ../blog
    git init
  2. Set the package name in composer.json. module:make defaults to <composer.vendor>/<lowercase name>.

    composer.json
    {
    "name": "acme/blog",
    "description": "Blog module"
    }

    Keep the generated autoload and autoload-dev entries so Composer loads the classes from vendor/acme/blog/app:

    composer.json
    "autoload": {
    "psr-4": {
    "Modules\\Blog\\": "app/",
    "Modules\\Blog\\Database\\Factories\\": "database/factories/",
    "Modules\\Blog\\Database\\Seeders\\": "database/seeders/"
    }
    }
  3. Declare any package dependencies under require in the same composer.json. Both Composer and module:update read it.

  4. Commit, tag, and push a release.

    Terminal window
    git add .
    git commit -m "Initial release"
    git tag v1.0.0
    git push origin HEAD --tags
  5. Publish the repository on Packagist, or register a private VCS repository in each consuming application:

    Terminal window
    composer config repositories.acme-blog vcs https://github.com/acme/blog

The package does not scan vendor/ by default. Enable scanning in the consuming application.

  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 in config/modules.php.

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

    Use base_path('vendor/acme/*') to narrow the scan to your own vendor folder. See Load modules from extra locations for path matching rules.

  3. Remove any local Modules/Blog folder first, then install the package.

    Terminal window
    composer require acme/blog:^1.0
  4. Enable the module. Scanned modules start disabled until listed in modules_statuses.json (unless an earlier entry for Blog already exists there).

    Terminal window
    php artisan module:enable Blog

To ship updates later, tag and push a new version (for example v1.0.1), then run composer update acme/blog in the application.

Terminal window
php artisan module:list

Blog appears as enabled with its path under vendor/acme/blog.