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.
Prepare the package
Section titled “Prepare the package”-
Move the module into its own Git repository. The folder already has
module.jsonandcomposer.jsonat its root.Terminal window cp -R Modules/Blog ../blogcd ../bloggit init -
Set the package name in
composer.json.module:makedefaults to<composer.vendor>/<lowercase name>.composer.json {"name": "acme/blog","description": "Blog module"}Keep the generated
autoloadandautoload-deventries so Composer loads the classes fromvendor/acme/blog/app:composer.json "autoload": {"psr-4": {"Modules\\Blog\\": "app/","Modules\\Blog\\Database\\Factories\\": "database/factories/","Modules\\Blog\\Database\\Seeders\\": "database/seeders/"}} -
Declare any package dependencies under
requirein the samecomposer.json. Both Composer andmodule:updateread it. -
Commit, tag, and push a release.
Terminal window git add .git commit -m "Initial release"git tag v1.0.0git push origin HEAD --tags -
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
Load the package in an application
Section titled “Load the package in an application”The package does not scan vendor/ by default. Enable scanning in the consuming application.
-
Publish
config/modules.phpif you have not already.Terminal window php artisan vendor:publish --provider="Nwidart\Modules\LaravelModulesServiceProvider" --tag=config -
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. -
Remove any local
Modules/Blogfolder first, then install the package.Terminal window composer require acme/blog:^1.0 -
Enable the module. Scanned modules start disabled until listed in
modules_statuses.json(unless an earlier entry forBlogalready 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.
Check the result
Section titled “Check the result”php artisan module:listBlog appears as enabled with its path under vendor/acme/blog.