Skip to content

Autoloading and the Composer merge plugin

Module classes load through Composer. This page explains how and why the package uses a Composer plugin. For setup steps, see Installation.

A module holds classes in a namespace such as Modules\Blog\. PHP finds those classes through Composer’s autoloader, so Composer needs to know the namespace and directory. There are three ways to configure it.

Before version 11, the application’s composer.json used a single entry:

"autoload": {
"psr-4": {
"Modules\\": "Modules/"
}
}

This requires no plugin and no composer.json edits when adding modules. However, it forces every module namespace to mirror its folder tree from Modules/ down.

With the default app/ layout, Modules\Blog\Http\Controllers\X lives in Modules/Blog/app/Http/Controllers/X.php, so this entry cannot find it. Modules also cannot define their own autoload rules or move out as standalone packages unchanged.

One PSR-4 entry per module, written by hand

Section titled “One PSR-4 entry per module, written by hand”

You can add one entry per module in the application’s composer.json, such as "Modules\\Blog\\": "Modules/Blog/app/". This supports the default layout without a plugin.

The cost is manual maintenance. You must edit the root composer.json for every module, duplicating what the module already declares. Composer also ignores each module’s own autoload and autoload-dev maps (factories, seeders, tests), so you must copy those entries into the root file too.

Since version 11, each module declares its own autoload map in its composer.json, and the application merges them using wikimedia/composer-merge-plugin:

"extra": {
"merge-plugin": {
"include": [
"Modules/*/composer.json"
]
}
}

When Composer builds the autoloader, the plugin reads every matching file and merges its sections into the root config. Because this package requires the plugin, Composer installs it automatically and asks once whether to trust it in allow-plugins. If allow-plugins sets the plugin to false, Composer skips it and module classes do not autoload.

The composer.json generated by module:make maps Modules\Blog\ to app/ and adds entries for factories, seeders, and tests. See Anatomy of a module.

What the merge does with a module composer.json

Section titled “What the merge does with a module composer.json”

The plugin merges autoload, autoload-dev, require, and require-dev (merge-dev is enabled by default). A module can therefore declare its own package dependencies. The plugin skips extra and scripts unless you enable merge-extra or merge-scripts. See the plugin README for all options.

module:update handles what the merge plugin leaves out. It copies a module’s scripts into the root composer.json. It also runs composer require (or --dev) for module dependencies so Composer installs them right away instead of waiting for composer update. See module:update.

Composer writes its autoload map to vendor/composer/ when you run it. Rebuild the root autoloader after adding a module manually, copying a module folder, or editing a module’s composer.json. module:make runs composer dump-autoload in the application root for you.

module:dump does not rebuild the application autoloader. It runs composer dump -o -n -q inside each module folder, leaving the root map unchanged. See module:dump.

Module service providers listed in module.json are classes too, so they must autoload before the package can register them.

module:make --plain writes no composer.json. The merge plugin glob matches nothing for that module, so its classes do not autoload and it has no provider. Add a composer.json with an autoload map and list a provider in module.json when you need them.

One Modules\ entry One entry per module, by hand Merge plugin
Setup One line in composer.json One entry per module A plugin and an include entry
New module No composer.json change Edit composer.json, rebuild autoloader Rebuild autoloader (module:make runs it)
Layout Namespace equals folder tree; breaks default app/ layout Free Free; each module maps its own namespaces
Module-level autoload (factories, seeders, tests) Not possible Copied by hand Merged from the module
Module as a package Needs a rewrite Module composer.json works, but root copy can drift Module composer.json works as-is
Extra dependency None None wikimedia/composer-merge-plugin, trusted in allow-plugins

The plugin executes code during Composer commands, so some teams choose not to trust it. Use manual per-module entries if you prefer to avoid it. Composer still installs the plugin as a dependency of this package.